在 python 中复制一个文件

Duplicate a file in python

我正在尝试复制 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