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


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 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,332 Feb-10-2020, 03:13 PM
Last Post: buran
  Command line inputs not printing to Log File ijosefson 1 3,381 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