사용자가 지정한 파일을 복사하고 복사본을 만들어야합니다 (사용자가 지정한 이름 지정). 이것은 내 코드입니다.파이썬에서 파일을 복사하는 방법은 무엇입니까?
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의 내용이 인쇄되지 않으므로 이상하게 보입니다.
사용'이 중복처럼 보이는 – mgilson
을 shutil.copy'를,이 체크 아웃 : http://stackoverflow.com/questions/123198/how-do-i-copy-a- 파일 - 인 - 파이썬 –
@MichaelW 고마워요! –