简体   繁体   English

替换 python 中的多个文件和文件夹

[英]Replace multiple files and folders in python

How could I go about doing it?我怎么能 go 关于这样做? I have a big folder with a lot of sub-folders and files, I want to replace multiple specific folders and files with other folders and files stored somewhere else that contain the same file structure.我有一个包含很多子文件夹和文件的大文件夹,我想用其他文件夹和文件替换多个特定文件夹和文件,这些文件夹和文件存储在包含相同文件结构的其他位置。

BigFolder
|
|--Folder 1
|--Folder 2
|--Folder 3
|--Folder 4
|--File 1
|--File 2


"Backup 1"
|
|--Folder 1
|--Folder 3
|--File 2

"Backup 2"
|
|--Folder 1
|--Folder 3
|--File 2

"Backup 3"
|
|--Folder 1
|--Folder 3
|--File 2

I put "backup" in quotes because it's not the real purpose of this, but for example purposes it works.我将“备份”放在引号中,因为这不是真正的目的,但出于示例目的,它可以工作。 So I should be able to grab the specific contents of the big folder and put them into one of the backups, then take another backup and grab its contents and drop them into the big folder, basically swapping them.所以我应该能够抓取大文件夹的特定内容并将它们放入其中一个备份中,然后进行另一个备份并抓取其内容并将它们放入大文件夹中,基本上是交换它们。 How could I achieve this?我怎么能做到这一点? I tried with shutil.copytree but that throws an error because the folder I'm copying into already exists, really dumb limitation.我尝试使用 shutil.copytree 但这会引发错误,因为我要复制到的文件夹已经存在,非常愚蠢的限制。 I also tried with distutils' copytree, but while it works, it doesn't copy the folders I want, just the contents, so it doesn't preserve the file structure.我也尝试过使用 distutils 的 copytree,但是虽然它可以工作,但它不会复制我想要的文件夹,只是复制内容,因此它不会保留文件结构。

import shutil

bigFolder = "/home/user/Big Folder"

def swapFiles(newFolder, prevFolder):
    path = os.path
    #Copy from big folder to the previous backup
    shutil.copytree(path.join(bigFolder, "Folder 1"), prevFolder)
    shutil.copytree(path.join(bigFolder, "Folder 3"), prevFolder)
    shutil.copy(path.join(bigFolder, "File 2"), prevFolder)

    #Copy from new folder to big folder
    shutil.copytree(newFolder, bigFolder)

This doesn't work because it the destination folder already exists, it only works on non existing folder.这不起作用,因为它的目标文件夹已经存在,它只适用于不存在的文件夹。 With distutils:使用 distutils:

from distutils.dir_util import copy_tree
import shutil

bigFolder = "/home/user/Big Folder"

def swapFiles(newFolder, prevFolder):
    path = os.path
    #Copy from big folder to the previous backup
    copy_tree(path.join(bigFolder, "Folder 1"), prevFolder)
    copy_tree(path.join(bigFolder, "Folder 3"), prevFolder)
    shutil.copy(path.join(bigFolder, "File 2"), prevFolder) #shutil is fine here

    #Copy from new folder to big folder
    copy_tree(newFolder, bigFolder)

This doesn't work because it copies the contents of the folders and not the folder itself, so I don't keep the file structure.这不起作用,因为它复制文件夹的内容而不是文件夹本身,所以我不保留文件结构。

By the way, in the middle of the two copying processes I should remove the files and folders to avoid merging with the newFolder's ones, but that's irrelevant to the question.顺便说一句,在两个复制过程的中间,我应该删除文件和文件夹以避免与 newFolder 的合并,但这与问题无关。

#recursively merge two folders including subfolders
def mergefolders(root_src_dir, root_dst_dir):
    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):
                os.remove(dst_file)
            shutil.copy(src_file, dst_dir)

source: https://lukelogbook.tech/2018/01/25/merging-two-folders-in-python/来源: https://lukelogbook.tech/2018/01/25/merging-two-folders-in-python/

I found a solution, turns out shutil's stupid limitation of only being able to copy files to folders that don't exist was useful after all, but I also needed to use the copy_tree function from distutils, luckily they are both stdlib.我找到了一个解决方案,结果是shutil 的愚蠢限制,即只能将文件复制到不存在的文件夹,这毕竟是有用的,但我还需要使用distutils 的copy_tree function,幸运的是它们都是stdlib。 First, create a list with all the files and folders you want to copy over as so:首先,创建一个包含您要复制的所有文件和文件夹的列表,如下所示:

filesToCopy = ["File 2", "Folder 1", "Folder 3"]

Then we need to create a couple of loops for each of the stages.然后我们需要为每个阶段创建几个循环。

import shutil, os
from distutils.dir_util import copy_tree

def swapFiles(newDir, prevDir):
    bigFolder = "Big Folder/Path"

    #Copy from the big folder to the prevDir
    for file in filesToCopy:
    #Check if what you're about to copy is a directory
    if os.path.isdir(os.path.join(bigFolder, file)):
        copy_tree(os.path.join(bigFolder, file), os.path.join(prevDir, file))
    else:
        #If its a file, do a simple copy
        shutil.copy(os.path.join(bigFolder, file), os.path.join(prevDir, file))

    #Remove the copies folders from the big folder
    for file in filesToCopy:
        if os.path.isdir(os.path.join(bigFolder, file)):
            shutil.rmtree(os.path.join(bigFolder, file))
        else:
            os.remove(os.path.join(bigFolder, file))

    #Copy over files from newDir to bigFolder
    for file in filesToCopy:
        if os.path.isdir(os.path.join(newDir, file)):
            #I use shutil's copytree here because I want it to create the folders I deleted in the previous step
            shutil.copytree(os.path.join(newDir, file), os.path.join(bigFolder, file))
        else:
            shutil.copy(os.path.join(newDir, file), os.path.join(bigFolder, file))

With this, the file structure is maintained, and it's really easy to specify which files and folders you want to swap.有了这个,文件结构得到维护,并且很容易指定要交换的文件和文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM