gpt4 book ai didi

python - 将文件复制到新位置并递增文件名,python

转载 作者:太空宇宙 更新时间:2023-11-04 10:55:31 24 4
gpt4 key购买 nike

<分区>

我正在尝试:

  1. 遍历一堆文件
  2. 做出一些改变
  3. 将旧文件复制到子目录。如果文件已经存在,我不想覆盖新目录中的文件。 (例如,如果“Filename.mxd”已经存在,则复制并重命名为“Filename_1.mxd”。如果“Filename_1.mxd”存在,则将文件复制为“Filename_2.mxd”等等...)
  4. 保存文件(但要保存,而不是另存为,以便覆盖现有文件)

它是这样的:

for filename in glob.glob(os.path.join(folderPath, "*.mxd")):
fullpath = os.path.join(folderPath, filename)

mxd = arcpy.mapping.MapDocument(filename)

if os.path.isfile(fullpath):
basename, filename2 = os.path.split(fullpath)


# Make some changes to my file here

# Copy the in memory file to a new location. If the file name already exists, then rename the file with the next instance of i (e.g. filename + "_" + i)

for i in range(50):
if i > 0:
print "Test1"
if arcpy.Exists(draftloc + "\\" + filename2) or arcpy.Exists(draftloc + "\\" + shortname + "_" + str(i) + extension):
print "Test2"
pass
else:
print "Test3"
arcpy.Copy_management(filename2, draftloc + "\\" + shortname + "_" + str(i) + extension)
mxd.save()

因此,我决定做的两件事就是将文件范围设置得远远超出我预期的范围 (50)。我确信有更好的方法可以做到这一点,只需递增到下一个数字而不设置范围。

第二件事,如您所见,脚本会保存范围内的所有内容。我只想在下一个没有发生的 i 实例上保存它一次。

希望这是有道理的,

迈克

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com