简体   繁体   English

如何使用python检查路径是否存在并复制文件夹和文件

[英]how to check if path exist and copy folders and files using python

I have a python script that must check first if folder exist in giving path then if exist delete the existing one then copy the new one from the source.我有一个 python 脚本,它必须首先检查给定路径中是​​否存在文件夹,如果存在则删除现有的,然后从源复制新的。 if does't exist just copy the new folder.如果不存在,只需复制新文件夹。

the problem is that in the source path if i have folder the system will check if the folder exist in the destination and delete and copy if exist.问题是,在源路径中,如果我有文件夹,系统将检查目标中是否存在该文件夹,如果存在则删除并复制。

what i want is to just check for one folder name "test" and then if does't exist copy the new folder if exist delete and copy.我想要的是只检查一个文件夹名称“test”,然后如果不存在则复制新文件夹,如果存在则删除并复制。

code:代码:

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()
  1. why do you even check?你为什么还要检查? Just rmtree the destination and ignore the error if it doesn't exist.只需 rmtree 目标,如果错误不存在,则忽略该错误。 You're not getting any significant saving by first checking, you're just making your code more complex.通过第一次检查,您并没有获得任何显着的节省,您只是让您的代码更加复杂。

  2. why do you delete src for every folder you're copying?为什么要为要复制的每个文件夹删除 src? Just delete it once before the loop只需在循环删除一次

  3. also you should probably copy the sub-folders of src in sub-folders of dst rather than dump everything in src您也应该将 src 的子文件夹复制到 dst 的子文件夹中,而不是将所有内容都转储到 src

  4. os.walk will recursively walks through all the directories under the root (and thus their directories, and theirs, ...), that really doesn't seem to be what you want here, os.walk 将递归遍历根目录下的所有目录(以及它们的目录,以及它们的目录......),这似乎不是你想要的,

  5. your path management is weird as hell, why do you have two different sources and three destinations used completely inconsistently?你的路径管理很奇怪,为什么你有两个不同的来源和三个完全不一致的目的地?

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

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