Question: 999 The following python 3 code opens a text file, write to the text file and then reads and prints the text file contents Under
999 The following python 3 code opens a text file, write to the text file and then reads and prints the text file contents
Under Windows 7 Pro Notepad a blank testfile.txt is created and saved on the desktop (testfile.txt)
Under Python 3.6.5 Edit with IDLE3.6 a .py file is created ( test.py) and is saved on the desktop
The code is placed in the test.py file and then tested via:
Edit with IDLE 3.6 / Run / Run Module F5
Nothing is written to the text file (it remains blank)
Nothing can be read from the blank file
$ cat creates a syntax error
print file.read() creates a syntax error
Please correct the below code to write to the text file, then display it via $ cat, then print it via file.read()
#test.py
file=open("testfile.txt","w") file.write("Hello World") file.write("This is our new text file") file.write("and this is another line") file.write("Why, because we can")
file=close()
#Open the text file to see the contents
$ cat testfile.text
#Open and print contents of test file file=open("testfile.txt","r") print file.read()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
