Art*_*ros 64 python file move overwrite
我有一个目录,'Dst Directory',里面有文件和文件夹,我有'src Directory',里面还有文件和文件夹.我想要做的是将'src Directory'的内容移动到'Dst Directory'并覆盖存在同名的任何文件.因此,例如'Src Directory\file.txt'需要移动到'Dst Directory \'并覆盖现有的file.txt.这同样适用于某些文件夹,移动文件夹并将内容与'dst目录'中的相同文件夹合并
我目前正在使用shutil.move将src的内容移动到dst,但是如果文件已经存在并且它不会合并文件夹,它将不会这样做; 它只是将文件夹放在现有文件夹中.
更新:使事情更清晰; 我正在做的是将存档解压缩到Dst目录,然后在那里移动Src目录的内容并重新压缩,有效地更新zip存档中的文件.这将重复添加新文件或新版本的文件等,这就是它需要覆盖和合并的原因
解决:我通过使用distutils.dir_util.copy_tree(src,dst)解决了我的问题,这将文件夹和文件从src目录复制到dst目录并覆盖/合并需要的地方.希望能帮助一些人!
希望有道理,谢谢!
Ray*_*ega 56
这将通过源目录,创建目标目录中尚不存在的任何目录,并将文件从源移动到目标目录:
import os
import shutil
root_src_dir = 'Src Directory\\'
root_dst_dir = 'Dst Directory\\'
for src_dir, dirs, files in os.walk(root_src_dir):
dst_dir = src_dir.replace(root_src_dir, root_dst_dir, 1)
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
for file_ in files:
src_file = os.path.join(src_dir, file_)
dst_file = os.path.join(dst_dir, file_)
if os.path.exists(dst_file):
# in case of the src and dst are the same file
if os.path.samefile(src_file, dst_file):
continue
os.remove(dst_file)
shutil.move(src_file, dst_dir)
Run Code Online (Sandbox Code Playgroud)
os.remove
在被相应的源文件替换之前,将首先(通过)删除任何预先存在的文件.目标中已存在但未在源中存在的任何文件或目录将保持不变.
Bra*_*des 49
copy()
而是使用,它愿意覆盖目标文件.如果你想让第一棵树消失,rmtree()
那么一旦你完成迭代就把它分开.
http://docs.python.org/library/shutil.html#shutil.copy
http://docs.python.org/library/shutil.html#shutil.rmtree
更新:
os.walk()
在源树上做一个.对于每个目录,检查它是否存在于目标端,os.makedirs()
如果缺少则存在.对于每个文件,只需简单地shutil.copy()
创建或覆盖文件,以适当的方式.
您可以使用它来复制目录并覆盖现有文件:
import shutil
shutil.copytree("src", "dst", dirs_exist_ok=True)
Run Code Online (Sandbox Code Playgroud)
dirs_exist_ok
Python 3.8 中添加了参数。
请参阅文档:https://docs.python.org/3/library/shutil.html#shutil.copytree
由于以上都不适用于我,所以我编写了自己的递归函数.调用函数copyTree(dir1,dir2)来合并目录.在多平台Linux和Windows上运行.
def forceMergeFlatDir(srcDir, dstDir):
if not os.path.exists(dstDir):
os.makedirs(dstDir)
for item in os.listdir(srcDir):
srcFile = os.path.join(srcDir, item)
dstFile = os.path.join(dstDir, item)
forceCopyFile(srcFile, dstFile)
def forceCopyFile (sfile, dfile):
if os.path.isfile(sfile):
shutil.copy2(sfile, dfile)
def isAFlatDir(sDir):
for item in os.listdir(sDir):
sItem = os.path.join(sDir, item)
if os.path.isdir(sItem):
return False
return True
def copyTree(src, dst):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isfile(s):
if not os.path.exists(dst):
os.makedirs(dst)
forceCopyFile(s,d)
if os.path.isdir(s):
isRecursive = not isAFlatDir(s)
if isRecursive:
copyTree(s, d)
else:
forceMergeFlatDir(s, d)
Run Code Online (Sandbox Code Playgroud)
如果您还需要使用只读标志覆盖文件,请使用以下命令:
def copyDirTree(root_src_dir,root_dst_dir):
"""
Copy directory tree. Overwrites also read only files.
:param root_src_dir: source directory
:param root_dst_dir: destination directory
"""
for src_dir, dirs, files in os.walk(root_src_dir):
dst_dir = src_dir.replace(root_src_dir, root_dst_dir, 1)
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
for file_ in files:
src_file = os.path.join(src_dir, file_)
dst_file = os.path.join(dst_dir, file_)
if os.path.exists(dst_file):
try:
os.remove(dst_file)
except PermissionError as exc:
os.chmod(dst_file, stat.S_IWUSR)
os.remove(dst_file)
shutil.copy(src_file, dst_dir)
Run Code Online (Sandbox Code Playgroud)