【发布时间】:2020-10-02 00:36:42
【问题描述】:
我正在尝试将几千个 pdf 从一个文件位置移动到另一个位置。源文件夹包含多个子文件夹,我仅将 pdf(技术图纸)合并到一个文件夹中,以简化对团队其他成员的搜索。
主要目标是仅复制目标文件夹中尚不存在的文件。我尝试了几个不同的选项,最近的选项如下所示,并且在所有情况下,每次都会复制每个文件。在今天之前,每当我尝试批量文件移动时,如果文件存在于目标文件夹中,我会收到错误消息,但我不再这样做了。
我已验证其中一些文件存在于两个位置,但仍在被复制。有什么我遗漏的或可以修改以纠正的吗?
感谢您的帮助。
import os.path
import shutil
source_folder = os.path.abspath(r'\\source\file\location')
dest_folder = os.path.abspath(r'\\dest\folder\location')
for folder, subfolders, files in os.walk(source_folder):
for file in files:
path_file=os.path.join(folder, file)
if os.path.exists(file) in os.walk(dest_folder):
print(file+" exists.")
if not os.path.exists(file) in os.walk(dest_folder):
print(file+' does not exist.')
shutil.copy2(path_file, dest_folder)
【问题讨论】:
-
@DanielWalker 感谢您查看此内容。我以为
if os.path.exists(file) in os.walk(dest_folder):正在检查文件是否存在于目标文件夹中,但从你的问题来看,这听起来不正确。
标签: python-3.x shutil os.path