我需要复制用户指定的文件并复制它(给它一个用户指定的名称)。这是我的代码:如何在Python中复制文件?
import copy
def main():
userfile = raw_input('Please enter the name of the input file.')
userfile2 = raw_input('Please enter the name of the output file.')
infile = open(userfile,'r')
file_contents = infile.read()
infile.close()
print(file_contents)
userfile2 = copy.copy(file_contents)
outfile = open(userfile2,'w+')
file_contents2 = outfile.read()
print(file_contents2)
main()
这里发生了一些奇怪的事情,因为它不打印第二个文件outfile的内容。
使用'shutil.copy' – mgilson 2013-03-05 18:59:05
这看起来像一个重复的,检查了这一点:http://stackoverflow.com/questions/123198/how-do-i-copy-a- file-in-python – 2013-03-05 19:30:12
@MichaelW谢谢! – 2013-03-06 01:42:38