批量複製跳出文件存在怎麼跳過去(标签-list|关键词-input)

批量複製時總是會出現已經存在文件 怎麼跳過去

input_dir = input("輸入搜索的目錄")
output_dir = input("輸入保存文件目錄")
files = list()
Ofiles = list()
newfiles = list()
search_dir(output_dir,Ofiles)
search_dir(input_dir, files)
for file in files:
for file1 in Ofiles:
if file != file1:
if os.path.isfile(file) and file.endswith('.pdf'):

            #print(file)
            newfiles.append(file)
            #print(file)

#print(*newfiles, sep='c')
for newfile in newfiles:
print('find %s'%(newfile))
if os.path.isfile(newfile) and file.endswith('.pdf'):
#如果文件是個pdf文件就打印文件
print('move %s' % (newfile))
shutil.move(newfile, output_dir)

img

你可以用os.listdir()获取目录文件夹下的文件名,你在复制过来前线判断一下,目录文件夹是不是有这个文件了

判断文件存在与否,好像是这个os.path.exists
或者try包一下shutil.move(newfile, output_dir)

如果文件存在但是需要覆盖的话,使用copyfile试试,然后再把源文件删了。