使用python将目录内容复制到目录中 [英] Copy directory contents into a directory with python

查看:114
本文介绍了使用python将目录内容复制到目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录/a/b/c,其中包含文件和子目录. 我需要在/x/y/z目录中复制/a/b/c/*.我可以使用哪些python方法?

I have a directory /a/b/c that has files and subdirectories. I need to copy the /a/b/c/* in the /x/y/z directory. What python methods can I use?

我尝试了shutil.copytree("a/b/c", "/x/y/z"),但是python尝试创建/x/y/z并引发了error "Directory exists".

I tried shutil.copytree("a/b/c", "/x/y/z"), but python tries to create /x/y/z and raises an error "Directory exists".

推荐答案

我发现此代码有效:

from distutils.dir_util import copy_tree

# copy subdirectory example
fromDirectory = "/a/b/c"
toDirectory = "/x/y/z"

copy_tree(fromDirectory, toDirectory)

参考:

  • Python 2: https://docs.python.org/2/distutils/apiref.html#distutils.dir_util.copy_tree
  • Python 3: https://docs.python.org/3/distutils/apiref.html#distutils.dir_util.copy_tree

这篇关于使用python将目录内容复制到目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆