I’m going through Zed Shaw’s online course Learn Python the Hard Way and I ran into a study drill that gave me some trouble.
In lesson 15 the seventh study drill is a little tricky if you’re not familiar with the command line. Instead of opening a Python file, you’re instructed to enter the commands from the Python prompt.
Just running open(example.txt)
may not work because the Python interpreter only looks in the current folder to find the file. If you don’t start Python in the same folder, you need to tell the interpreter exactly where the file is is located. Here’s one way to do this:
$ python
>>> txt = open("/Users/Kevin/Dev/example.txt")
>>> print txt.read()
If you’re not sure if you’re in the right directory, you can double check your directory. First start Python:
$ python
Then type these two commands:
>>> import os
>>> os.getcwd()
Remember to always import the os module before calling any os commands/functions.
Your result should look like this: