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?
我有一个目录/a/b/c,有文件和子目录。我需要在/x/y/z目录中复制/a/b/c/*。我可以使用什么python方法?
I tried shutil.copytree("a/b/c", "/x/y/z")
, but python tries to create /x/y/z and raises an error "Directory exists"
.
我试着shutil。copytree(“a/b/c”、“/x/y/z”),但是python尝试创建/x/y/z,并引发一个错误“目录存在”。
4 个解决方案
#1
79
I found this code working.
我发现这段代码有效。
from distutils.dir_util import copy_tree
# copy subdirectory example
fromDirectory = "/a/b/c"
toDirectory = "/x/y/z"
copy_tree(fromDirectory, toDirectory)
- Reference - https://docs.python.org/2/distutils/apiref.html#distutils.dir_util.copy_tree
- 引用——https://docs.python.org/2/distutils/apiref.html distutils.dir_util.copy_tree
#2
1
You can also use glob2 to recursively collect all paths (using ** subfolders wildcard) and then use shutil.copyfile, saving the paths
您还可以使用glob2来递归地收集所有路径(使用**子文件夹通配符),然后使用shutil。拷贝文件,保存路径
glob2 link: https://code.activestate.com/pypm/glob2/
glob2链接:https://code.activestate.com/pypm/glob2/
#3
0
from subprocess import call
def cp_dir(source, target):
call(['cp', '-a', source, target]) # Linux
cp_dir('/a/b/c/', '/x/y/z/')
It works for me. Basically, it executes shell command cp.
它适合我。基本上,它执行shell命令cp。
#4
-5
The python libs are obsolete with this function. I've done one that works correctly:
这个函数已经过时了。我做了一个正确的工作:
import os
import shutil
def copydirectorykut(src, dst):
os.chdir(dst)
list=os.listdir(src)
nom= src+'.txt'
fitx= open(nom, 'w')
for item in list:
fitx.write("%s\n" % item)
fitx.close()
f = open(nom,'r')
for line in f.readlines():
if "." in line:
shutil.copy(src+'/'+line[:-1],dst+'/'+line[:-1])
else:
if not os.path.exists(dst+'/'+line[:-1]):
os.makedirs(dst+'/'+line[:-1])
copydirectorykut(src+'/'+line[:-1],dst+'/'+line[:-1])
copydirectorykut(src+'/'+line[:-1],dst+'/'+line[:-1])
f.close()
os.remove(nom)
os.chdir('..')
#1
79
I found this code working.
我发现这段代码有效。
from distutils.dir_util import copy_tree
# copy subdirectory example
fromDirectory = "/a/b/c"
toDirectory = "/x/y/z"
copy_tree(fromDirectory, toDirectory)
- Reference - https://docs.python.org/2/distutils/apiref.html#distutils.dir_util.copy_tree
- 引用——https://docs.python.org/2/distutils/apiref.html distutils.dir_util.copy_tree
#2
1
You can also use glob2 to recursively collect all paths (using ** subfolders wildcard) and then use shutil.copyfile, saving the paths
您还可以使用glob2来递归地收集所有路径(使用**子文件夹通配符),然后使用shutil。拷贝文件,保存路径
glob2 link: https://code.activestate.com/pypm/glob2/
glob2链接:https://code.activestate.com/pypm/glob2/
#3
0
from subprocess import call
def cp_dir(source, target):
call(['cp', '-a', source, target]) # Linux
cp_dir('/a/b/c/', '/x/y/z/')
It works for me. Basically, it executes shell command cp.
它适合我。基本上,它执行shell命令cp。
#4
-5
The python libs are obsolete with this function. I've done one that works correctly:
这个函数已经过时了。我做了一个正确的工作:
import os
import shutil
def copydirectorykut(src, dst):
os.chdir(dst)
list=os.listdir(src)
nom= src+'.txt'
fitx= open(nom, 'w')
for item in list:
fitx.write("%s\n" % item)
fitx.close()
f = open(nom,'r')
for line in f.readlines():
if "." in line:
shutil.copy(src+'/'+line[:-1],dst+'/'+line[:-1])
else:
if not os.path.exists(dst+'/'+line[:-1]):
os.makedirs(dst+'/'+line[:-1])
copydirectorykut(src+'/'+line[:-1],dst+'/'+line[:-1])
copydirectorykut(src+'/'+line[:-1],dst+'/'+line[:-1])
f.close()
os.remove(nom)
os.chdir('..')