簡體   English   中英

Python:從多個位置復制目錄,而不是文件,如果名稱相同,則覆蓋

[英]Python: Copy directories, not files, from multiple locations, overwriting if same name

我有一個主文件夾netbooks_nbo,其中包含更多帶日期的文件夾。 我想獲取最后七個文件夾(按上次修改日期)並將它們復制到C:\\驅動器上的某個位置。 這是我當前的代碼:

代碼如下:

import os
import distutils.core

def get_immediate_subdirectories(dir):
    return [os.path.join(dir, name) for name in os.listdir(dir)
            if os.path.isdir(os.path.join(dir, name))]

def main():
    path = "\\\\Network_Drive\\netbooks_nbo"
    all_dirs = get_immediate_subdirectories(path)
    all_dirs.sort(key=lambda x: os.path.getmtime(x))
    all_dirs = all_dirs[len(all_dirs)-7: len(all_dirs)]

    for i in all_dirs:
        for n in get_immediate_subdirectories(i):
            distutils.dir_util.copy_tree(n, "C:\\AllFiles")
            print "copied"+ n

問題是dir_util.copy_tree復制所有文件,而不是實際目錄。 我想保留目錄結構。 我嘗試使用shutil.copytree(src, dst)但它只返回錯誤,因為C:\\ AllFiles在for循環的一次迭代后就已經存在。 由於某些奇怪的權限錯誤shutil.copy(src,dst)不起作用。

有任何想法嗎?

如果目錄樹不是太大,則可以將每個目錄樹打包到一個存檔文件中 ,然后將每個存檔文件解壓縮到目標位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM