2024-07-08 15:56:04 发布
网友
我有一个目录/a/b/c,其中有文件和子目录。 我需要复制/x/y/z目录中的/a/b/c/*。我可以使用哪些python方法?
我试过shutil.copytree("a/b/c", "/x/y/z"),但python尝试创建/x/y/z并引发一个error "Directory exists"。
shutil.copytree("a/b/c", "/x/y/z")
error "Directory exists"
from subprocess import call def cp_dir(source, target): call(['cp', '-a', source, target]) # Linux cp_dir('/a/b/c/', '/x/y/z/')
对我有用。基本上,它执行shell命令。
我发现这个代码有效。
from distutils.dir_util import copy_tree # copy subdirectory example fromDirectory = "/a/b/c" toDirectory = "/x/y/z" copy_tree(fromDirectory, toDirectory)
参考:
还可以使用glob2递归地收集所有路径(使用**子文件夹通配符),然后使用shuil.copyfile保存路径
glob2链接:https://code.activestate.com/pypm/glob2/
对我有用。基本上,它执行shell命令。
我发现这个代码有效。
参考:
还可以使用glob2递归地收集所有路径(使用**子文件夹通配符),然后使用shuil.copyfile保存路径
glob2链接:https://code.activestate.com/pypm/glob2/
相关问题 更多 >
编程相关推荐