【问题标题】:how to check if path exist and copy folders and files using python如何检查路径是否存在并使用python复制文件夹和文件
【发布时间】:2020-02-04 11:42:21
【问题描述】:

我有一个 python 脚本,它必须首先检查文件夹是否存在于给定路径中,如果存在则删除现有的,然后从源中复制新的。如果不存在,只需复制新文件夹即可。

问题是在源路径中,如果我有文件夹,系统将检查该文件夹是否存在于目标中,如果存在则删除并复制。

我想要的是只检查一个文件夹名称“test”,然后如果不存在则复制新文件夹,如果存在则删除并复制。

代码:

import os
import shutil
from os import path
import datetime


src = "I:/"
src2 = "I:/test"

dst = "C:/Users/LT GM/Desktop/test/"
dst2 = "C:/Users/LT GM/Documents/"
dst3 = "C:/Users/LT GM/Documents/Visual Studio 2017"

def main():
    copy()

def copy():
    #go through  the src files and folders 
    for root,dirs,files in os.walk(src):
        try:
            for folder in dirs:
                #return folder name
                full_folder_name = os.path.join(src, folder)
                print("folder : ",full_folder_name)
                #check if folder exits
                if os.path.exists(dst):
                    print("folder exist")
                    #delete folder
                    shutil.rmtree(dst)
                    print("the deleted folder is :{0}".format(dst))
                    #copy the folder as it is (folder with the files)
                    copieddst = shutil.copytree(src2,dst)
                    print("copy of the folder is done :{0}".format(copieddst))
                else:
                    print("folder does Not Exist")
                    #copy the folder as it is (folder with the files)
                    copieddst = shutil.copytree(src2,dst)
                    print("copy of the folder  is done :{0}".format(copieddst))
        except Exception as e:
            print(e)

        try:
            for name in files:
                full_file_name = os.path.join(src, name)
                print("files: ",full_file_name)
                #check for pdf extension
                if name.endswith("pdf"):
                    #copy files
                    shutil.copy(full_file_name, dst2)

                    #check for doc & docx extension 
                elif name.endswith("docx") or name.endswith("doc"):
                    #copy files
                    shutil.copy(full_file_name, dst3)
            print("word files done")
            print("pdf files done")
        except Exception as e:
            print(e)
if __name__=="__main__":
    main()

【问题讨论】:

  • 您不必检查目录是否存在,它会在您的except中打印错误

标签: python shutil os.path


【解决方案1】:
  1. 你为什么还要检查?只需 rmtree 目标,如果它不存在则忽略错误。第一次检查并没有显着节省,只是让代码更复杂。

  2. 为什么要删除要复制的每个文件夹的 src?只需在 循环之前删除一次

  3. 您也应该将 src 的子文件夹复制到 dst 的子文件夹中,而不是将所有内容都转储到 src 中

  4. os.walk 将递归遍历根目录下的所有目录(以及它们的目录,以及它们的...),这似乎不是您想要的,

  5. 你的路径管理很奇怪,为什么你有两个不同的来源和三个目的地使用完全不一致?

【讨论】:

  • 你的评论是写的,这就是我问这个问题的原因,我问的是第二点。对于 4 你建议我使用什么?对于 5 2 源是文件夹和子文件夹。 3 个来源,因为我想复制文件夹、pdf 文件、word 文件。到三个不同的目的地。
  • 对于 (4) 只需使用 listdir 或 scandir,或 pathlib 的 iterdir/glob 方法。对于 (5)... 好吧,我想这会受益于更好的命名,并且您将以多个副本结束。
猜你喜欢
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
相关资源
最近更新 更多