python 复制文件夹内容 并结构一致_Python-移动和覆盖文件和文件夹
Python-移動和覆蓋文件和文件夾
我有一個目錄“ Dst Directory”,其中包含文件和文件夾,而我有“ src Directory”,其中也包含文件和文件夾。 我要做的是將“ src目錄”的內容移動到“ Dst目錄”,并覆蓋具有相同名稱的所有文件。 因此,例如需要將“ 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目錄,并在需要時覆蓋/合并。 希望對某些人有所幫助!
希望有道理,謝謝!
6個解決方案
51 votes
這將遍歷源目錄,創(chuàng)建目標目錄中尚不存在的任何目錄,并將文件從源移動到目標目錄:
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)
任何先前存在的文件都將首先被刪除(通過os.remove),然后由相應的源文件替換。 目標中已存在但源中不存在的所有文件或目錄將保持不變。
Ray Vega answered 2019-10-06T09:05:07Z
44 votes
請改用os.walk(),它愿意覆蓋目標文件。 如果您隨后希望第一棵樹消失,則在完成對它的遍歷后,只需分別2557098390990995862529就可以了。
[http://docs.python.org/library/shutil.html#shutil.copy]
[http://docs.python.org/library/shutil.html#shutil.rmtree]
更新:
在源代碼樹上執(zhí)行os.walk()。 對于每個目錄,請檢查目標目錄中是否存在該目錄,如果缺少,請檢查os.makedirs()。 對于每個文件,只需shutil.copy(),然后將創(chuàng)建或覆蓋該文件,以適當的方式為準。
Brandon Rhodes answered 2019-10-06T09:04:35Z
6 votes
由于以上都不適合我,因此我編寫了自己的遞歸函數。 調用函數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)
ALLOY answered 2019-10-06T09:05:33Z
3 votes
如果還需要用只讀標志覆蓋文件,請使用以下命令:
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)
Vit Bernatik answered 2019-10-06T09:05:57Z
1 votes
看一下:os.remove刪除現有文件。
phimuemue answered 2019-10-06T09:06:22Z
0 votes
我有一個類似的問題。 我想移動文件和文件夾結構并覆蓋現有文件,但不刪除目標文件夾結構中的任何內容。
我通過使用shutil.move(),遞歸調用我的函數并在要覆蓋的文件和不存在的文件夾上使用shutil.move()來解決了該問題。
它的工作方式類似于shutil.move(),但好處是現有文件只會被覆蓋,而不會被刪除。
import os
import shutil
def moverecursively(source_folder, destination_folder):
basename = os.path.basename(source_folder)
dest_dir = os.path.join(destination_folder, basename)
if not os.path.exists(dest_dir):
shutil.move(source_folder, destination_folder)
else:
dst_path = os.path.join(destination_folder, basename)
for root, dirs, files in os.walk(source_folder):
for item in files:
src_path = os.path.join(root, item)
if os.path.exists(dst_file):
os.remove(dst_file)
shutil.move(src_path, dst_path)
for item in dirs:
src_path = os.path.join(root, item)
moverecursively(src_path, dst_path)
the answered 2019-10-06T09:07:02Z
總結
以上是生活随笔為你收集整理的python 复制文件夹内容 并结构一致_Python-移动和覆盖文件和文件夹的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux中如何使用tmpfs内存文件系
- 下一篇: python 按键精灵识图_利用-百度云