【问题标题】:Copy file to a different location and changing the filename at the same time将文件复制到其他位置并同时更改文件名
【发布时间】:2018-10-09 11:32:47
【问题描述】:
我想将文件复制到其他位置并同时更改文件名:
from shutils import copyfile, copy
path = os.path.join(dst, 'file_new_name.xls')
copyfile(src, path) # or
copy(src, path)
我得到一个FileNotFoundError,路径为:dst\old_filename\newfilename。
这是因为我尝试复制为不同的文件名。
【问题讨论】:
标签:
python
python-3.x
copy
file-rename
shutil
【解决方案1】:
您应该将旧文件路径的目录名与新文件名连接起来:
path = os.path.join(os.path.dirname(dst), 'file_new_name.xls')
【解决方案2】:
我想最快的方法是在这里使用它:
首先,您使用这一行在文档顶部导入 fs
var fs = require('fs');
您可以像这样使用它来将文件复制到新位置并同时更改名称。
fs.createReadStream('FirstLocationFile.txt').pipe(fs.createWriteStream('SecondLocationFile.txt'));
您当然也可以将文件夹路径添加到字符串中以描述它们是否在或应该在文件夹中。