对于我的 QGIS 插件,我想编写一个 python 脚本,如果该目录已经存在,则在目录名称后添加一个序列号,与 Windows 在多次下载同一个文件时的方式相同。但我不知道如何实现这一点。
例如:
如果该目录'C:/Dossier'
已经存在,则新目录将是'C:/Dossier(1)'
等等。
先感谢您。
我认为您需要这样的东西,path_to_dir
您要创建的目标在哪里。
import os
count=0
if os.path.exists(path_to_dir):
# start while (adding "(count)" to string)
while True:
new_dir_name=path_to_dir+f'({count})'
if os.path.exists(new_dir_name):
count+=1
else:
os.mkdir(new_dir_name)
break
else:
os.mkdir(path_to_dir)