python - 在python中复制文件

标签 python file rename file-rename

我正在尝试在 python 中复制文件。

通过 Shutil,我只找到了复制和移动文件的解决方案,但我需要复制文件来重命名并保留原始文件。

    photos54_TempString = "photos54/thumb-" + artistName + "_54.jpg"
    photos54_NewString = "quickManik_Music/" + songID + "-" + 
    artistName + "_54.jpg"

    copyfile(photos54_TempString, photos54_NewString)

最佳答案

使用函数shutil.copyfile .

这是一个使用终端和 Python 解释器的示例:

$ cd /tmp
$ touch myfile
$ python3
Python 3.5.3 (default, May 10 2017, 15:05:55) 
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.copyfile('myfile', 'mysecondfile')
'mysecondfile'
>>> 
$ ls my*
myfile  mysecondfile

关于python - 在python中复制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44768275/

相关文章:

c# - 图像调整大小 asp.net mvc 应用程序

python - 将 python stdout 重定向到文件失败并出现 UnicodeEncodeError

r - 如何使用字符对象使用 dplyr 重命名 R 中的列?

python - 使用python将目录中的所有文件重命名为每个文件中存在的行

python - 是否可以在 Emacs 中将 Ropemacs 与 TRAMP 一起使用?

python - 返回语句困惑..我不确定语法

python - Python中使用unittest的相对导入

python - 使用同一数据框中其他两列的值从一列创建二维列表

file - 新的音频文件格式

mercurial - 正在显示 Mercurial 状态的重命名?