I run this from eclipse, the name of the file I'm working with is ex16_text.txt (yes, I type it correctly. It writes the file correctly (input appears), but "print txt.read ()" does nothing (prints blank line), see the output after the code:
filename = raw_input("What the file name we'll be working with?")
print "we're going to erase %s" % filename
print "opening the file"
target = open(filename, 'w')
print "erasing the file"
target.truncate()
print "give me 3 lines to replace file contents:"
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print "writing lines to file"
target.write(line1+"\n")
target.write(line2+"\n")
target.write(line3)
txt = open(filename)
print "here are the contents of the %s file:" % filename
print txt.read()
target.close()
Conclusion:
What file name will we work with? ex16_text.txt we will delete ex16_text.txt opening the file erasing the file give me 3 lines to replace the contents of the file: line 1: three line 2: two line 3: one record of lines in the file here is the contents of the file ex16_text.txt:
source
share