Saq*_*eel 9 python operating-system os.walk shutil
目前,我正在处理一个同步两个文件夹的项目。我在以下示例中的文件夹将 ad Folder_1 命名为源,将Folder_2 命名为目标我想做以下事情。
我已经完成了第一点的一半,其中我能够将文件从 Folder_1 复制到 Folder_2。发送部分我可以将文件从 Folder_2 复制到 folder_1 的部分仍然存在。
以下是我的代码
import os, shutil
path = 'C:/Users/saqibshakeel035/Desktop/Folder_1/'
copyto = 'C:/Users/saqibshakeel035/Desktop/Folder_2/'
files =os.listdir(path)
files.sort()
for f in files:
src = path+f
dst = copyto+f
try:
if os.stat(src).st_mtime < os.stat(dst).st_mtime:
continue
except OSError:
pass
shutil.copy(src,dst)#this is the case when our file in destination doesn't exist
=
print('Files copied from'+ path +'to' + copyto+ '!')
Run Code Online (Sandbox Code Playgroud)
我可以修改或执行哪些操作才能完全同步两个文件夹?提前致谢 :)
MPa*_*el1 16
(与您的方法不同,但按您的查询预期完成工作)
简单的代码使用dirsync:
from dirsync import sync
source_path = '/Give/Source/Folder/Here'
target_path = '/Give/Target/Folder/Here'
sync(source_path, target_path, 'sync') #for syncing one way
sync(target_path, source_path, 'sync') #for syncing the opposite way
Run Code Online (Sandbox Code Playgroud)
有关更多选项,请参阅此处的文档:dirsync - PyPI
当然,如果需要,您可以手动添加异常处理。
| 归档时间: |
|
| 查看次数: |
11265 次 |
| 最近记录: |