. Advertisement .
. Advertisement .
On UNIX and UNIX-like systems, you can set file path in Python with ease. However, this isn’t the case with Windows. Find out why and what you can do to overcome the problem.
File Paths In Operating Systems
On Windows systems, you will have to deal with traditional DOS paths most of the time. A standard path can have up to three components:
- A letter indicating the drive or volume, followed by a separator (a semicolon).
- The directory name in the nested hierarchy. The backslash (/) character is typically used to separate directories in this path.
- The filename (optional). It is separate from the file path with the directory separator character.
When there are all three components, they form an absolute path. If the drive or volume letter isn’t present, you have an absolute path from the current drive’s root. Otherwise, it is a relative path from the current directory.
A relative path on Windows may have a relative directory segment like “.” (which indicates the current directory) or “..” (the parent directory).
Unix and Unix-like systems also have their own directory tree hierarchy. Their definitions of absolute and relative paths are similar to those of Windows systems. However, there is a major difference in file paths between those operating systems.
While Windows uses the backslash (
/) as the delimiting character between directories, POSIX systems like Unix use the (forward) slash (
/) for the job.
Set File Path In Python
In general, you can just use a string to represent your file path in POSIX systems.
>>> import os.path as path >>> path.exists('/usr/bin') True
This also works for relative paths:
>>> path.exists('./ITTutoria') True
Windows users, however, may run into some minor problems. In Python strings, the backslash (
\) is a special character, or to be more specific, the escape character.
When used in conjunction with another character, it invokes a different interpretation of that character. The combination is also known as an escape sequence.
Like other programming languages, Python uses these escape sequences for special characters, such as control characters. For instance,
\n represents a newline character,
' – single quote,
\t – tab,
\b – backspace, etc.
That is why you will have problems with the backslash in a Windows path. Python doesn’t interpret it as the delimiting character between directories by default, and you may see wrong results like this:
>>> path.exists('D:\new_project') False
Python will see \n and treat it as the escape sequence for a newline character instead of your correct path. As a result, the
exists() function can’t detect your directory and returns False.
With String Literals
There are several ways to solve this. You can use two backslashes (
\\) as the escape sequence for a backslash itself. When Python reads ‘
\\n‘, it only interprets the substring as ‘
\n‘, not a newline character anymore.
>>> path.exists('D:\\new_project') True
To avoid the trouble, you can also use a string literal instead.
>>> path.exists(r'D:\new_project') True
When prefixed with an ‘r’ or ‘R’ character, your sequence of characters becomes a string literal, and Python will ignore escape sequences like ‘\n’.
join() function allows you to join several components to make a valid file path. It returns the concatenation of those components, added with the correct separator of your operating system.
>>> import os.path as path >>> filepath = path.join('D:', 'new_project') >>> path.exists(filepath) True
The pathlib module is available on Python version 3.4 or above. It comes with several tools for dealing with filesystem paths, including the Path class. Instances of this class represent paths of your operating system in an intelligent way.
>>> from pathlib import Path >>> filepath = Path('D:\new_project') >>> path.exists(filepath) True
On Windows, you can use a string literal, the
os.path.join() function, or the pathlib.Path class to set file path in Python. They stop Python from interpreting the backslash as an escape character and lead to unexpected results. Knowing this will help you, for example, add Python to PATH on Windows easier.