python - Why does my script write every input string twice to the output file? -
when @ wrote it's double. example if write 'dog' ill 'dogdog'. why?
reading , writing file, filename taken command line arguments:
from sys import argv script,text=argv def reading(f): print f.read() def writing(f): print f.write(line) filename=open(text) #opening file reading(filename) filename.close() filename=open(text,'w') line=raw_input() filename.write(line) writing(filename) filename.close()
as said output getting double value of input giving.
you getting double value because writing 2 times
1) function call
def writing(f): print f.write(line)
2) writing in file using filename.write(line)
use code:
from sys import argv script,text=argv def reading(f): print f.read() def writing(f): print f.write(line) filename=open(text,'w') line=raw_input() writing(filename) filename.close()
and no need close file 2 times, once finished read , write operations close it.
Comments
Post a Comment