Oct-16-2017, 06:23 AM
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
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
Thanks
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.


Thanks
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.