我想从文件中复制文本并将文本写入另一个文件

时间:2012-07-09 07:52:59

标签: python

fob = open("LISA designshpe.txt", "r+")

foc = open ("n.txt","w+")
s = fob.readlines()

fob.close()

for a in s :

    line =foc.write( a )

foc.close()

2 个答案:

答案 0 :(得分:1)

应该适用于Python 2.7及更高版本:

with open("infilename") as infile, open("outfilename", 'w') as outfile:
  # outfile.write(infile.read()) # read entire file at once
  outfile.writelines(line for line in infile) # read file line by line

或只是复制文件:

import shutil
shutil.copy("infilename", "outfilename")

参考文献:

答案 1 :(得分:0)

只需使用writelines()

foc.writelines(s)