首先要在这里提到的是python,这是我的新手。
现在我有一个文件位于:
a/long/long/path/to/file.py
我想复制到我的主目录,并创建一个新文件夹:
/home/myhome/new_folder
我的预期结果是:
/home/myhome/new_folder/a/long/long/path/to/file.py
有没有现成的图书馆可以做到这一点?如果没有,我该如何实现?
要创建os.makedirs()
在复制之前可以使用的所有中级目标目录:
import os
import shutil
srcfile = 'a/long/long/path/to/file.py'
dstroot = '/home/myhome/new_folder'
assert not os.path.isabs(srcfile)
dstdir = os.path.join(dstroot, os.path.dirname(srcfile))
os.makedirs(dstdir) # create all directories, raise an error if it already exists
shutil.copy(srcfile, dstdir)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句