Categories

# Manipulating File Paths with Python

Python is a convenient language that’s often used for scripting, data science, and web development.

# Files and File Paths

A file has a filename to reference the file. It also has a path to locate the file’s location.

The path consists of the folder, they can be nested and they form the path.

# Backslash on Windows and Forward Slash on macOS and Linux

In Windows, the path consists of backslashes. In many other operating systems like macOS and Linux, the path consists of forward slashes.

Python’s standard pathlib library knows the difference and can sort them out accordingly. Therefore, we should use it to construct paths so that our program will run everywhere.

For instance, we can import pathlib as follows and create a Path object as follows:

from pathlib import Path
path = Path('foo', 'bar', 'foo.txt')


After running the code, path should be a Path object like the following if we’re running the program above on Linux or macOS:

PosixPath('foo/bar/foo.txt')


If we’re running the code above on Windows, we’ll get a WindowsPath object instead of a PosixPath object.

# Using the / Operator to Join Paths

We can use the / operator to join paths. For instance, we can rewrite the path we had into the following code:

from pathlib import Path
path = Path('foo')/'bar'/'foo.txt'


Then we get the same result as before.

This will also work on Windows, macOS, and Linux since Python will sort out the path accordingly.

What we shouldn’t use is the string’s join method because the path separator is different between Windows and other operating systems.

For instance:

path = '/'.join(['foo', 'bar', 'foo.txt'])


isn’t going to work on Windows since the path has forward slash.

# The Current Working Directory

We can get the current working directory (CWD), which is the directory the program is running on.

We can change the CWD with the os.chdir function and get the current CWD with the Path.cwd function.

For instance, we can write:

from pathlib import Path
import os
print(Path.cwd())
os.chdir(Path('foo')/'bar')
print(Path.cwd())


Then we get:

/home/runner/AgonizingBasicSpecialist
/home/runner/AgonizingBasicSpecialist/foo/bar


as the output.

As we can see, chdir changed the current working directory, so that we can use manipulate files in directories other than the ones that the program is running in.

# The Home Directory

The home directory is the root directory of the profile folder of the user’s user account.

For instance, we can write the following:

from pathlib import Path
path = Path.home()


Then the value of path is something likePosixPath(‘/home/runner’) .

# Absolute vs. Relative Paths

An absolute path is a path that always begins with the root folder. A relative is a path that’s relative to the program’s current working directory.

For example, on Windows, C:\Windows is an absolute path. A relative path is something like .\foo\bar . It starts with a dot and foo is inside the current working directory.

# Creating New Folders Using the os.makedirs() Function

We can make a new folder with the os.makedirs function.

For instance, we can write:

from pathlib import Path
Path(Path.cwd()/'foo').mkdir()


Then we make a foo directory inside our current working directory.

# Handling Absolute and Relative Paths

We can check if a path is an absolute path with the is_absolute method.

For instance, we can write:

from pathlib import Path
is_absolute = Path.cwd().is_absolute()


Then we should see is_absolute being True since Path.cwd() returns an absolute path.

We can call os.path.abspath to returns a string with of the absolute path of the path argument that we pass in.

For instance, given that we have the directory foo in the current working directory, we can write:

from pathlib import Path
import os
path = os.path.abspath(Path('./foo'))


to get the absolute path of the foo folder.

We then should get something like:

'/home/runner/AgonizingBasicSpecialist/foo'


as the value of path .

os.path.isabs(path) is a method that returns True is a path that is absolute.

The os.path.relpath(path, start) method will return a string of the relative path from the start path to path .

If start isn’t provided, then the current working directory is used as the start path.

For instance, if we have the folder /foo/bar in our home directory, then we can get the path of ./foo/bar relative to the home directory by writing:

from pathlib import Path
import os
path = os.path.relpath(Path.home(), Path('./foo')/'bar')


Then the path has the value ‘../../..’ .

# Conclusion

We can use the path and os modules to construct and manipulate paths.

Also, we can also use the / with Path objects to create a path that works with all operating systems.

We can also path in paths to the Path function to construct paths.

Python also has methods to check for relative and absolute paths and the os module can construct relative paths from 2 absolute paths.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.