Мне нужно скопировать файл, указанный пользователем, и сделать его копию (указав ему имя, указанное пользователем). Это мой код:Как скопировать файл в 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()
Что-то странное происходит здесь, так как он не печатает содержимое второго файла, выходной_файл.
использование 'shutil.copy' – mgilson
Это выглядит как дубликат, проверить это: http://stackoverflow.com/questions/123198/how-do-i-copy-a- file-in-python –
@MichaelW Спасибо! –