Go to: Na-Rae Han's home page  

Python 3 Notes

        [ HOME | LING 1330/2330 ]

File Path and CWD

<< Previous Note           Next Note >>
On this page: open(), file path, CWD ('current working directory'), r 'raw string' prefix, os.getcwd(), os.chdir().

Referencing a File with a Full Path and Name

As seen in Tutorials #12 and #13, you can refer to a local file in Python using the file's full path and file name. Below, you are opening up a file for reading:
 
>>> myfile = open('C:/Users/narae/Desktop/alice.txt')   # Windows
>>> mytxt = myfile.read()
>>> myfile.close()
 
>>> myfile = open('/Users/narae/Desktop/alice.txt')     # Mac and Linux
>>> mytxt = myfile.read()
>>> myfile.close()
In Windows, a full file directory path starts with a drive letter (C:, D:. etc.). In Linux and OS-X, it starts with "/", which is called root. Directories are separated by a slash "/". You can look up a file's full directory path and file name through its "Properties". See how it is done in this FAQ.

Referencing a File in Windows

In Windows, there are a couple additional ways of referencing a file. That is because natively, Windows file path employs the backslash "\" instead of the slash. Python allows using both in a Windows system, but there are a couple of pitfalls to watch out for. To sum them up:
  • Python lets you use OS-X/Linux style slashes "/" even in Windows. Therefore, you can refer to the file as 'C:/Users/narae/Desktop/alice.txt'. RECOMMENDED.
  • If using backslash, because it is a special character in Python, you must remember to escape every instance: 'C:\\Users\\narae\\Desktop\\alice.txt'
  • Alternatively, you can prefix the entire file name string with the rawstring marker "r": r'C:\Users\narae\Desktop\alice.txt'. That way, everything in the string is interpreted as a literal character, and you don't have to escape every backslash.

File Name Shortcuts and CWD (Current Working Directory)

So, using the full directory path and file name always works; you should be using this method. However, you might have seen files called by their name only, e.g., 'alice.txt' in Python. How is it done?

The concept of Current Working Directory (CWD) is crucial here. You can think of it as the folder your Python is operating inside at the moment. So far we have been using the absolute path, which begins from the topmost directory. But if your file reference does not start from the top (e.g., 'alice.txt', 'ling1330/alice.txt'), Python assumes that it starts in the CWD (a "relative path").

This means that a name-only reference will be successful only when the file is in your Python's CWD. But bear in mind that your CWD may change. Also, your Python has different initial CWD settings depending on whether you are working with a Python script or in a shell environment.

  • In a Python script:
    When you execute your script, your CWD is set to the directory where your script is. Therefore, you can refer to a file in a script by its name only provided that the file and the script are in the same directory. An example:
    myfile = open('alice.txt')  # alice.txt is in the same dir as foo.py
    mytxt = myfile.read()
    myfile.close()
    foo.py 
    
  • In Python shell:
    In your shell, the initial CWD setting varies by system. In Windows, the default location is often 'C:/program Files (x86)/Python35-32' (which is inconvenient -- see this "Basic Configurations" page or this FAQ for how to change it). In OS-X, it is usually '/Users/username/Documents' where username is your user ID. (Mac users should see this FAQ for how to change your setting.) Unless your file happens to be in your CWD, you have two options:
    1. Change your CWD to the file's directory, or
    2. Copy or move your file to your CWD. (Not recommended, since your shell's CWD may change.)
See this screen shot and and the next section for how to work with your CWD setting in Python shell.

Finding and Changing CWD

Python module os provides utilities for displaying and modifying your current working directory. Below illustrates how to find your CWD (.getcwd()) and change it into a different directory (.chdir()). Below is an example for the windows OS:
 
>>> import os
>>> os.getcwd()
'D:\\Lab'
>>> os.chdir('scripts/gutenberg') # relative path: scripts dir is under Lab
>>> os.getcwd()
'D:\\Lab\\scripts\\gutenberg'
>>> os.chdir(r'D:\Corpora\corpus_samples') # absolute path, using \ and r prefix
>>> os.getcwd()
'D:\\Corpora\\corpus_samples'
Note that the CWD returned by Python interpreter is in the Windows file path format: it uses the backslash "\" for directory separator, and every instance is escaped. While Python lets Windows users use Linux/OS-X style "/" in file paths, internally it uses the OS-native file path format.