(Oct-20-2017, 04:46 AM)gruntfutuk Wrote: When you open a file your cursor position is at the beginning of the file so you can read from that position. When you write to a file the cursor position is at the end if what you have written. Unless you move the cursor (closing and then opening the file or using seek) you cannot read what you've just written.
Ahh, ok. So to do what I was trying to do I would have had to:
open it-
write it-
close it-
and then open it again to get it to the point where it could be read again??? Wow! what a pain in the
Or I could have used your example, but to use your example where would it go in the script? before the print and close commands???
Ok I added the
text_file.seek(0)
before the print and close commands and now it is working.Now I understand and thank you all for your assistance!
print('Creating a text file with the write() method.') text_file = open('write_it.txt', 'w+') text_file.write('Line 1\n') text_file.write('This is line 2\n') text_file.write('That makes this line 3\n') text_file.seek(0) print(text_file.read()) text_file.close() print('\nCreating a text file with the writelines() method.') text_file = open('write_it.txt', 'w+') lines = ['Line 1\n', 'This is line 2\n', 'That makes this line 3\n'] text_file.writelines(lines) text_file.seek(0) print(text_file.read()) text_file.close() print('\nReading the newly created file.') text_file = open('write_it.txt', 'r') print(text_file.read()) text_file.close()and my new output
Output:Creating a text file with the write() method.
Line 1
This is line 2
That makes this line 3
Creating a text file with the writelines() method.
Line 1
This is line 2
That makes this line 3
Reading the newly created file.
Line 1
This is line 2
That makes this line 3
In the grand scheme of things this is definitely in the trivial category, but if it helps me or maybe even someone else to learn something I don't think it can be considered pointless or useless.
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.