简体   繁体   中英

Python: folder creation when copying files

I'm trying to create a shell script that will copy files from one computer (employee's old computer) to another (employee's new computer). I have it to the point where I can copy files over, thanks to the lovely people here, but I'm running into a problem - if I'm going from, say, this directory that has 2 files:

C:\\Users\\specificuser\\Documents\\Test Folder ....to this directory... C:\\Users\\specificuser\\Desktop ...I see the files show up on the Desktop, but the folder those files were in (Test Folder) isn't created.

Here is the copy function I'm using:

#copy function
def dir_copy(srcpath, dstpath):
    #if the destination path doesn't exist, create it
    if not os.path.exists(dstpath):
        os.makedir(dstpath)
    #tag each file to the source path to create the file path
    for file in os.listdir(srcpath):
        srcfile = os.path.join(srcpath, file)
        dstfile = os.path.join(dstpath, file)
        #if the source file path is a directory, copy the directory
        if os.path.isdir(srcfile):
            dir_copy(srcfile, dstfile)
        else: #if the source file path is just a file, copy the file
            shutil.copyfile(srcfile, dstfile)

I know I need to create the directory on the destination, I'm just not quite sure how to do it.

Edit: I found that I had a type (os.makedir instead of os.mkdir). I tested it, and it creates directories like it's supposed to. HOWEVER I'd like it to create the directory one level up from where it's starting. For example, in Test Folder there is Sub Test Folder. It has created Sub Test Folder but won't create Test Folder because Test Folder is not part of the dstpath. Does that make sense?

You might want to look at shutil.copytree() . It performs the recursive copy functionality, including directories, that you're looking for. So, for a basic recursive copy, you could just run:

shutil.copytree(srcpath, dstpath)

However, to accomplish your goal of copying the source directory to the destination directory, creating the source directory inside of the destination directory in the process, you could use something like this:

import os
import shutil

def dir_copy(srcpath, dstdir):
    dirname = os.path.basename(srcpath)
    dstpath = os.path.join(dstdir, dirname)
    shutil.copytree(srcpath, dstpath)

Note that your srcpath must not contain a slash at the end for this to work. Also, the result of joining the destination directory and the source directory name must not already exist, or copytree will fail.

This is a common problem with file copy... do you intend to just copy the contents of the folder or do you want the folder itself copied. Copy utilities typically have a flag for this and you can too. I use os.makedirs so that any intermediate directories are created also.

#copy function
def dir_copy(srcpath, dstpath, include_directory=False):
    if include_directory:
        dstpath = os.path.join(dstpath, os.path.basename(srcpath))
    os.makedirs(dstpath, exist_ok=True)
    #tag each file to the source path to create the file path
    for file in os.listdir(srcpath):
        srcfile = os.path.join(srcpath, file)
        dstfile = os.path.join(dstpath, file)
        #if the source file path is a directory, copy the directory
        if os.path.isdir(srcfile):
            dir_copy(srcfile, dstfile)
        else: #if the source file path is just a file, copy the file
            shutil.copyfile(srcfile, dstfile)
import shutil
import os

def dir_copy(srcpath, dstpath):
    try:
        shutil.copytree(srcpath, dstpath)
    except shutil.Error as e:
        print('Directory not copied. Error: %s' % e)
    except OSError as e:
        print('Directory not copied. Error: %s' % e)


dir_copy('/home/sergey/test1', '/home/sergey/test2')

I use this script to backup (copy) my working folder. It will skip large files, keep folder structure (hierarchy) and create destination folders if they don't exist.

import os
import shutil


for root, dirs, files in os.walk(the_folder_copy_from):
   for name in files:
      if os.path.getsize(os.path.join(root, name))<10*1024*1024:
          target=os.path.join("backup", os.path.relpath(os.path.join(root, name),start=the_folder_copy_from))
          print(target)
          os.makedirs(os.path.dirname(target),exist_ok=True)
          shutil.copy(src=os.path.join(root, name),dst=target)

print("Done")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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