2015-11-19 76 views
1

我试图创建一个shell脚本,将文件从一台计算机(员工的旧计算机)复制到另一台(员工的新计算机)。我已经到了可以复制文件的地步,这要感谢可爱的人在这里,但是我遇到了一个问题 - 如果我从这个目录有两个文件:Python:复制文件时创建文件夹

C :\ Users \ specificuser \ Documents \测试文件夹 ....到这个目录... C:\ Users \ specificuser \ Desktop ...我看到文件显示在桌面上,但文件夹这些文件是(测试文件夹)中未创建。

这里是我使用的复制功能:

#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) 

我知道我需要在目的地创建目录,我只是不太清楚如何去做。

编辑:我发现我有一个类型(os.makedir而不是os.mkdir)。我测试了它,它创建了它应该的目录。但是我希望它能从创建目录的一层开始创建目录。例如,在测试文件夹中有子测试文件夹。它创建了子测试文件夹,但不会创建测试文件夹,因为测试文件夹不是dstpath的一部分。那有意义吗?

+0

@PaulK。我已经把它作为函数的第一件事了。但是,我认为问题在于我的“目标”是桌面 - 我没有将Desktop \ Test Folder指定为目标,所以测试文件夹永远不会被创建。 –

+0

我道歉,我没有看到。所以问题是该副本不能递归地工作? –

+0

@ PaulK.I编辑了我原来的帖子,但真正的问题是,我希望它从我开始的地方上升一级。如果我将Documents \ Test文件夹中的所有内容都移动到桌面,我希望它也创建Desktop \ Test文件夹,即使技术上我没有将目标指定为Desktop \ Test Folder,但我指定了Desktop。 –

回答

2

你可能想看看shutil.copytree()。它执行您正在寻找的递归复制功能,包括目录。因此,一个基本的递归复制,你可以只运行:

shutil.copytree(srcpath, dstpath) 

然而,来实现自己的复制源目录到目标目录的目标,在此过程中创造的目标目录内的源目录,你可以使用这样的事情:

import os 
import shutil 

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

请注意,您必须srcpath不是在年底包含斜杠这个工作。此外,加入目标目录和源目录名称的结果必须不存在,否则copytree将失败。

+0

这工作完美。谢谢! –

+0

我得到“访问被拒绝” - 有没有办法使用提升的权限或什么东西运行shutil?也许有用户插入计算机管理员的用户名和密码? –

+0

@MasterModnar如果您使用管理员权限运行脚本,则不应该获得拒绝访问错误。不过,我真的不知道在Windows上编程python。如果您遇到写入错误,则可以考虑更新目标文件夹的权限,而不是以管理员身份运行。你也可以'除了IOError',并且如果errno是13(权限被拒绝),可能使用不同的目标。 – MPlanchard

0
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') 
0

这是文件复制的常见问题......你打算只复制文件夹的内容还是你想让文件夹本身复制。复制实用程序通常有一个标志,你也可以。我使用os.makedirs,以便还可以创建任何中间目录。

#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) 
+0

'include_directory'为函数做了什么,为什么将它设置为'False'?另外,你能向我解释'exist_ok = True'吗? –

+0

'include_directory'在复制文件之前更改算法以在目标中创建'srcpath'(“Temp Folder”)的基本名称。 '操作系统。makedirs'并且会创建多个子目录(假设dstpath是需要创建的“C:\ Users \ specificuser \ Desktop \ test1 \ copiedfiles”),并且如果目录已经存在,则'exist_ok'不会引发错误(不需要为os.path.exists测试)。在第一次调用时,设置'include_directory = True',但后来的递归调用不需要它。 – tdelaney

相关问题