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
#1
Hello all,

Continuing my self study I just created a simple program which was to "write" to a new text file. The lesson is demonstrating a few different ways to write the same thing to the same file. It explained that by using w access mode you can write to a file and in so doing overwrite any existing file with that name. After completing it the way the book asked. Not disbelieving but merely wanting to see each instance printed out for my own satisfaction, I attempted to change it to print out the new text between each new instance before it actually gets overwritten.
At first I added the new print statements but it generated an error. After a little troubleshooting I figured out that it was probably because of the w mode instead of the w+ mode. I made the correction and the error message[s] did go away. However, what I really wanted to ask is despite the changes I've made to the code, It's still only printing out one time at the very end and I was hoping that someone might be able to explain why it's not working the way I expected it to, which is to have the 3 lines of text print out at each print command.

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')

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)
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()


input('\n\nPress the enter key to exit.')
The only difference between my example above and the book's is that I added the extra 2 print commands print(text_file.read()) and changed w to w+ before closing the file and overwriting it again. Also when I tried to add the print command after the close it generated an error
Error:
Creating a text file with the write() method. Traceback (most recent call last): File "C:/Users/crackity/Desktop/MyCode/write_it.py", line 13, in <module> print(text_file.read()) ValueError: I/O operation on closed file. >>>
Here is my output without the error messages but also without printing the the text 2 additional times.

Output:
Creating a text file with the write() method. Creating a text file with the writelines() method. Reading the newly created file. Line 1 This is line 2 That makes this line 3 Press the enter key to exit. >>>
The only difference I can see is that the last operation is opening the .txt file with the r option. From what I've read the w+ is supposed to allow both read and write access.
Wall Wall Wall
Thanks
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#2
So, first of all this traceback does not come from this code - line 13 in your code is different than the one stated in the traceback message. Post the actual code and corresponding traceback.
Reply
#3
You are correct. That error message doesn't correspond with the actual example I posted. I was merely trying to show the error code I got when I tried to place the print(text_file.read()) after the file close command.

What I'm trying to understand is why the lines from the text file only printed once despite my adding additional print commands. It's as if they were ignored by the rest of the code and I was hoping someone could help me understand why that is.

Does it have something to do with the fact that in the first 2 instances those commands were actually writing to the text file as opposed to the last one which seems to work where it is looking at it with the r read option?

and if so am I misunderstaning the write/read capabilities with the w+ option?

Thanks.
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#4
Your first print does not work because your position in the file is already past the content you wrote.

You need a text_file.seek(0) before the print.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#5
(Oct-16-2017, 10:09 AM)Crackity Wrote: You are correct. That error message doesn't correspond with the actual example I posted. I was merely trying to show the error code I got when I tried to place the print(text_file.read()) after the file close command.


I don't see the point to post 26 lines of irrelevant code.
Also, if you closed the file, how do you expect to be able to read/write without opening it again. Same as to open a door, pass trough it, close the door behind you and then trying to pass trough it again, without opening it again  :-)
Reply
#6
You've misquoted me. I hold that trying to read a file after you've got to the end of it will not work.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#7
@gruntfutuk  Sorry, that is clearly OP quote - and that is what i wanted to quote. I have to see how does it happen that has been posted as your quote. Now edited.

OK - I found how it happens - if you select a text in someone's post, but click "Quote highlighted text" in a post by someone else, then the quote is attributed to the wrong person
Reply
#8
(Oct-16-2017, 11:21 AM)gruntfutuk Wrote: Your first print does not work because your position in the file is already past the content you wrote.

You need a text_file.seek(0) before the print.

Of course, wouldn't I have to print it after I write it? I can't print anything before I've written it because there would be nothing to print. Also it gave me an error when I tried to print it after closing the file...
text_file.seek(0) wasn't used in any part of this and it still printed after the last write command so why would that be necessary?
Please understand that I'm not trying to argue with those that I've asked for help from, but that makes no sense to me and I'm trying to understand. :D

(Oct-16-2017, 11:53 AM)buran Wrote: I don't see the point to post 26 lines of irrelevant code.
Also, if you closed the file, how do you expect to be able to read/write without opening it again. Same as to open a door, pass trough it, close the door behind you and then trying to pass trough it again, without opening it again  :-)

Both of the new print commands that I added in an attempt to print out the newly written lines before they were actually overwritten, were added after opening the file and before closing it again. If you look at the code again you should see that the same file is opened and closed 3 different times. So thank you for your reply but I don't think you understood my question. Smile
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#9
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.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  My Loop for printing text file goes to infinity Nomex 7 4,149 Feb-10-2020, 03:13 PM
Last Post: buran
  Command line inputs not printing to Log File ijosefson 1 3,316 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