Python shutil.copy working-但它';循环10次?
我已经写了一些代码来帮助我备份我的照片。 我决定使用打印功能让我知道什么时候备份了文件,这样我就可以以一种简单的方式查看大规模备份的进度 但是,当我使用打印功能时,我注意到,对于同一文件名的每个文件,文件显然被复制了10次Python shutil.copy working-但它';循环10次?,python,python-3.x,csv,copy,shutil,Python,Python 3.x,Csv,Copy,Shutil,我已经写了一些代码来帮助我备份我的照片。 我决定使用打印功能让我知道什么时候备份了文件,这样我就可以以一种简单的方式查看大规模备份的进度 但是,当我使用打印功能时,我注意到,对于同一文件名的每个文件,文件显然被复制了10次 for subdir, dirs, files in os.walk(incomingdir): for f in files: filepath = os.path.join(subdir,f)
for subdir, dirs, files in os.walk(incomingdir):
for f in files:
filepath = os.path.join(subdir,f)
f_name,f_ext = (os.path.splitext(f))
f_proj = (f_name.split('_')[0])
f_cam = (f_name.split('_')[1])
f_num = (f_name[-2:])
with open(barcode_csv, 'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
if f_proj == row[1]:
if row[4] == "ROW 4 VALUE":
shutil.copy(filepath,backupdir)
print(f, " copied to NAS.")
我从中得到的输出如下所示:
1911205D4_CAM01_01.cr2 copied to NAS.
1911205D4_CAM01_01.cr2 copied to NAS.
1911205D4_CAM01_01.cr2 copied to NAS.
1911205D4_CAM01_01.cr2 copied to NAS.
1911205D4_CAM01_01.cr2 copied to NAS.
1911205D4_CAM01_01.cr2 copied to NAS.
1911205D4_CAM01_01.cr2 copied to NAS.
1911205D4_CAM01_01.cr2 copied to NAS.
1911205D4_CAM01_01.cr2 copied to NAS.
1911205D4_CAM01_01.cr2 copied to NAS.
1911205D4_CAM01_02.cr2 copied to NAS.
1911205D4_CAM01_02.cr2 copied to NAS.
1911205D4_CAM01_02.cr2 copied to NAS.
1911205D4_CAM01_02.cr2 copied to NAS.
1911205D4_CAM01_02.cr2 copied to NAS.
1911205D4_CAM01_02.cr2 copied to NAS.
1911205D4_CAM01_02.cr2 copied to NAS.
1911205D4_CAM01_02.cr2 copied to NAS.
1911205D4_CAM01_02.cr2 copied to NAS.
1911205D4_CAM01_02.cr2 copied to NAS.
文件会按预期复制到指定目录。我只是想知道这是否表明它试图复制同一个文件10次,然后重复写入9次?我只是意识到这些备份上的大负载会造成太多的覆盖和进程减慢,从而给我的NAS驱动器带来不必要的压力
如果我不使用CSV,只使用:
print(filepath)
它可以在屏幕上很好地打印,只显示单个文件
有谁能告诉我为什么要打印10次?如果它真的复制了10倍?如果是这样,我将如何确保每个文件只发生一次
谢谢您正在为文件中的每一行复制文件。我假定您的文件有10行。查看您是否能够找到如何将
shutil.copy
从循环中移出csv\u阅读器中的行。该函数可能会有所帮助,但一个布尔变量可以查看您的条件是否满足,它可以完成这项工作。Hi Kent!太好了,非常感谢!我查看了它正在读取的特定CSV文件,您是对的。我已将其缩小并删除了重复的实例。再次感谢!