Reputation: 8457
I am a beginner in Python and am learning via online tutorials. On Study Drill #6 for example 15 on this page, I'm trying to open up the 'ex15_sample.txt' file that I have created and exists.
This is what it says to do:
Start 'python' to start the Python shell, and use 'open' from the prompt just like in this program. Notice how you can open files and run 'read' on them from within 'python'?
In the command prompt, I entered python
to start the Python shell and have tried typing in the following, which threw me an error:
open(ex15_sample.txt)
The error I get is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ex15_sample' is not defined
ex15_sample.txt
is a file that exists in the same folder. How am I supposed to use open
or any other command from the prompt? I am running Python 2.7.8 on Powershell, by the way.
Upvotes: 1
Views: 444
Reputation: 5194
The first argument of open
is a string containing the filename.
Just put the file name in quotation marks:
f = open('ex15_sample.txt')
Upvotes: 2