将文件复制到不同的位置并同时更改文件名

Copy file to a different location and changing the filename at the same time

我想将文件复制到不同的位置并同时更改文件名:

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.

发生这种情况是因为我尝试将其复制为不同的文件名。

您应该将旧文件路径的目录名加入新文件名中:

path = os.path.join(os.path.dirname(dst), 'file_new_name.xls')

我想最快的方法就是在这里使用它:

首先,您使用此行

在文档顶部导入 fs
var fs = require('fs');

然后您可以像这样使用它将文件复制到新位置并同时更改名称。

fs.createReadStream('FirstLocationFile.txt').pipe(fs.createWriteStream('SecondLocationFile.txt'));

您当然也可以将文件夹路径添加到字符串中以描述它们是否在或应该在文件夹中。