0
hello why don't print text? (python) output:[] [] []
import names if __name__=="__main__": txt1=open("1.txt","w+") for i in range(0,3): txt1.write("%s\n" % names.get_first_name()) txt2=open("2.txt","w+") for i in range(0,3): txt2.write("%s\n" % names.get_first_name()) txt3=open("3.txt","w+") for i in range(0,3): txt3.write("%s\n" % names.get_first_name()) print(txt1.readlines(),txt2.readlines(),txt3.readlines())
2 Answers
+ 4
When changing from writing to a file to reading from the same file, use seek(0) to jump back to the beginning of the file.
Also, make sure to close all those files to prevent data loss. It's better to use context managers when dealing with files:
with open('file.txt', 'w+') as file:
# do something
# file is closed automatically
+ 3
write("%s\n, yourstring)
The separator of arguments to a function is comma (,) not %