Js *_*Lim 49 python directory copy file
首先我要提到的是,我是python的新手.
现在我有一个文件位于:
a/long/long/path/to/file.py
Run Code Online (Sandbox Code Playgroud)
我想要创建一个新文件夹复制到我的主目录:
/home/myhome/new_folder
Run Code Online (Sandbox Code Playgroud)
我的预期结果是:
/home/myhome/new_folder/a/long/long/path/to/file.py
Run Code Online (Sandbox Code Playgroud)
有没有现成的图书馆呢?如果不是,我该怎么做?
jfs*_*jfs 71
要创建所有中间级目标目录,可以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)
Run Code Online (Sandbox Code Playgroud)
ewo*_*wok 25
看看shutil
.shutil.copyfile(src, dst)
将文件复制到另一个文件.
请注意,shutil.copyfile
不会创建尚不存在的目录.为此,使用os.makedirs
归档时间: |
|
查看次数: |
165915 次 |
最近记录: |