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 #15 and #16, 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')
>>> mytxt = myfile.read()
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.
>>> myfile = open('/Users/narae/Desktop/alice.txt')
>>> mytxt = myfile.read()
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.
See this screen shot and and the next section for how to work with your CWD setting in Python shell.
- 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')
mytxt = myfile.read()
- In Python shell:
In your shell, the initial CWD setting varies by system. In Windows, the default location is often 'C:/Python27' (which is inconvenient -- see this FAQ for how to change it). In OS-X, it is usually '/Users/username' where username is your user ID.
Unless your file happens to be in your CWD, you have two options:
- Change your CWD to the file's directory, or
- Copy or move your file to your CWD. (Not recommended, since your shell's CWD may change.)
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:
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.
>>> import os