python 复制文件夹内容 并结构一致_Python-移动和覆盖文件和文件夹
Python-移動(dòng)和覆蓋文件和文件夾
我有一個(gè)目錄“ Dst Directory”,其中包含文件和文件夾,而我有“ src Directory”,其中也包含文件和文件夾。 我要做的是將“ src目錄”的內(nèi)容移動(dòng)到“ Dst目錄”,并覆蓋具有相同名稱的所有文件。 因此,例如需要將“ Src Directory \ file.txt”移至“ Dst Directory \”并覆蓋現(xiàn)有的file.txt。 對于某些文件夾,移動(dòng)文件夾并將內(nèi)容與“ dst目錄”中的同一文件夾合并,也是如此
我當(dāng)前正在使用shutil.move將src的內(nèi)容移動(dòng)到dst,但是如果文件已經(jīng)存在并且不會(huì)合并文件夾,則不會(huì)這樣做。 它將只將該文件夾放在現(xiàn)有文件夾中。
更新:使事情更清晰; 我正在做的是將存檔解壓縮到Dst目錄,然后將Src目錄的內(nèi)容移到那里并重新壓縮,從而有效地更新zip存檔中的文件。 將重復(fù)此操作以添加新文件或文件的新版本等,這就是為什么它需要覆蓋和合并的原因
已解決:我通過使用distutils.dir_util.copy_tree(src,dst)解決了我的問題,這會(huì)將文件夾和文件從src目錄復(fù)制到dst目錄,并在需要時(shí)覆蓋/合并。 希望對某些人有所幫助!
希望有道理,謝謝!
6個(gè)解決方案
51 votes
這將遍歷源目錄,創(chuàng)建目標(biāo)目錄中尚不存在的任何目錄,并將文件從源移動(dòng)到目標(biāo)目錄:
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),然后由相應(yīng)的源文件替換。 目標(biāo)中已存在但源中不存在的所有文件或目錄將保持不變。
Ray Vega answered 2019-10-06T09:05:07Z
44 votes
請改用os.walk(),它愿意覆蓋目標(biāo)文件。 如果您隨后希望第一棵樹消失,則在完成對它的遍歷后,只需分別2557098390990995862529就可以了。
[http://docs.python.org/library/shutil.html#shutil.copy]
[http://docs.python.org/library/shutil.html#shutil.rmtree]
更新:
在源代碼樹上執(zhí)行os.walk()。 對于每個(gè)目錄,請檢查目標(biāo)目錄中是否存在該目錄,如果缺少,請檢查os.makedirs()。 對于每個(gè)文件,只需shutil.copy(),然后將創(chuàng)建或覆蓋該文件,以適當(dāng)?shù)姆绞綖闇?zhǔn)。
Brandon Rhodes answered 2019-10-06T09:04:35Z
6 votes
由于以上都不適合我,因此我編寫了自己的遞歸函數(shù)。 調(diào)用函數(shù)copyTree(dir1,dir2)合并目錄。 在多平臺(tái)Linux和Windows上運(yùn)行。
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
如果還需要用只讀標(biāo)志覆蓋文件,請使用以下命令:
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刪除現(xiàn)有文件。
phimuemue answered 2019-10-06T09:06:22Z
0 votes
我有一個(gè)類似的問題。 我想移動(dòng)文件和文件夾結(jié)構(gòu)并覆蓋現(xiàn)有文件,但不刪除目標(biāo)文件夾結(jié)構(gòu)中的任何內(nèi)容。
我通過使用shutil.move(),遞歸調(diào)用我的函數(shù)并在要覆蓋的文件和不存在的文件夾上使用shutil.move()來解決了該問題。
它的工作方式類似于shutil.move(),但好處是現(xiàn)有文件只會(huì)被覆蓋,而不會(huì)被刪除。
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
總結(jié)
以上是生活随笔為你收集整理的python 复制文件夹内容 并结构一致_Python-移动和覆盖文件和文件夹的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux中如何使用tmpfs内存文件系
- 下一篇: UEStudio是什么工具