我正在尝试从目录中迭代几个文件,然后我将它们(基于它们的初始名称)复制到一个特定位置,然后从当前目录中删除它们。但是因为我在将它们组合在一起后删除它们,所以当循环移动到下一个被删除的文件时,我得到一个文件未找到异常。我该如何解决它: 这是我的代码:
import os
import csv
import glob,fnmatch, shutil ,time
ftp_directory = 'C:\\CIRP\\Velocidata\\Test\\'
IFIND_LOCATION = 'C:\\CIRP\\Velocidata\\Test2\\'
# Getting the list of all files
for root, dirs, files in os.walk(ftp_directory):
# Filtering for group names that are 'Inprogress'
groups_beingworked = []
for name in files:
#Getting the exception in the below line in the second iteration
group = name[0:name.lower().find('infile')]
for loop2 in files:
if (loop2[:len(group)].lower() == group):
groups_beingworked.append(loop2)
for loop3 in groups_beingworked:
shutil.copy2(os.path.join(root,loop3) ,IFIND_LOCATION)
print ('Deleting the file : ' + loop3)
#File removed from the FTP location through a ftp conn created elsewhere
ftpconn.delete(os.path.join(root, loop3))
我要尝试的示例文件列表是:
file1.new.infile.type1
file1.new.infile.type1
file2.old.infile.type2
file1.new.infile.type1
file2.old.infile.type2
file3.rec.infile.type3