[Tutor] syntax error-unexpected EOF while parsing

loomer at gmx.com loomer at gmx.com
Tue Mar 10 00:39:37 EDT 2020


Hello,
I'm going through an online python course and in the video called "Basic
I/O with Files," I generated an error that I cannot figure out.

It starts with creating a .txt file in Jupyter Notebook...

	%%writefile test.txt
	line 1
	line 2
	line 3

I guess "%%Writefile" only works in JNotebook but it did create a file
called "test.txt" with those lines. I opened the actual file to check.

Then he says test the file with...

	myfile = open('test.txt')

which returns nothing indicating that the file exists.

OK, then...

	myfile.read()
	'line 1\nline 2\nline 3\n'

Then...

	myfile.seek(0)

to reset the cursor to 0, then...

	myfile.close()

to close it for the next part I guess.

The problem arises when I try the next part...

	with open('test.txt') as new_file:

which, from what I understand, creates a virtual file that you can add
lines to, right? OK, the error in JNotebook is...

	File "<ipython-input-105-5e0af1fc3fba>", line 1
     with open('test.txt') as new_file:
                                       ^
	SyntaxError: unexpected EOF while parsing

I tried using the path to the file...

	PWD
	'P:\\'
	myfile = open('P:\\test.txt')

and that works, but same error with...

	with open('P:\\test.txt') as new_file:

So I tried it in Atom and got a similar error...

	myfile = open('P:\\test.txt')
	myfile.read()
	myfile.seek(0)
	myfile.close()

	with open('P:\\test.txt') as test_virt:

	File "P:\test-txt-glitch.py", line 6
                                               ^
	SyntaxError: unexpected EOF while parsing
	[Finished in 0.668s] [love Atom lol]

The file only has 3 lines so I have no idea what's wrong. It works for
the instructor in the video and I've checked and rechecked my typing
against the video.
Thanks.



More information about the Tutor mailing list