Python Forum
Newly written .txt file not printing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newly written .txt file not printing
#10
(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 Naughty

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! Big Grin

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. Smile
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply


Messages In This Thread
Newly written .txt file not printing - by Crackity - Oct-16-2017, 06:23 AM
RE: Newly written .txt file not printing - by buran - Oct-16-2017, 07:11 AM
RE: Newly written .txt file not printing - by buran - Oct-16-2017, 11:53 AM
RE: Newly written .txt file not printing - by buran - Oct-16-2017, 12:36 PM
RE: Newly written .txt file not printing - by Crackity - Oct-20-2017, 04:55 AM
RE: Newly written .txt file not printing - by buran - Oct-20-2017, 06:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  My Loop for printing text file goes to infinity Nomex 7 4,411 Feb-10-2020, 03:13 PM
Last Post: buran
  Command line inputs not printing to Log File ijosefson 1 3,412 Oct-19-2017, 06:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020