AppDividend
Latest Code Tutorials

Python os.path.join: How to Join Paths in Python

The OS module in Python implements some valuable functions on pathnames. The os.path module is always the path module suitable for the operating system Python is running on, therefore usable for local paths. But what is the file path, and what its consist of?

What is in a file path?

The file path consists of a sequence of file and folder names. This sequence of names walks you to a specific place on your computer’s operating system (OS).

path = "/Users/krunal/Desktop/code/"

The above path takes us to the code folder or directory.

To get the path dynamically without hardcoding, use the os.path.join() method. The idea of os.path.join() is to make your program cross-platform. Let’s see this method in detail.

os.path.join

The os.path.join() is a built-in Python function that joins one or more path components. The os.path.join() function concatenates several path components with precisely one directory separator (‘/’) following each non-empty part minus the last path component.

If the last path segment to be joined is empty, then a directory separator (‘/’) is placed at the end.

The os.path.join() method is frequently used with os methods like os.walk() to create the final path for a file or folder. The os.path.join() automatically inserts any required forward slashes into the file pathname.

Syntax

os.path.join(path, *paths)

Parameters

path: A path-like object representing a file system path.

*path: A path-like object representing a file system path. It represents the path components to be joined.

Return Value

The os.path.join() method returns a string that represents the concatenated path components.

Example

Write the following code that demonstrates the path.join() method.

import os

path = "/Users/krunal/Desktop/code/"

print(os.path.join(path, "pyt", "database", "app.py"))

Output

/Users/krunal/Desktop/code/pyt/database/app.py

As you can see from the output that the os.path.join() function combines path names into one complete path. That means you can join multiple segments of a path into one instead of hard-coding every pathname manually.

To get the current working directory in Python, use the following file pathname.

import os

cwd = os.getcwd()

print(cwd)

Output

/Users/krunal/Desktop/code/pyt/database

The os.path.expanduser() method will expand the pathname that uses ~ to represent the current user’s home directory.

This works on any platform where users have a home directory, including Linux, MacOS, and Windows. The returned path does not have a trailing slash but the os.path.join() method doesn’t care.

Let’s not use the hardcoded path, and let’s use the Python functions to print the complete path up to the desired filename.

import os

cwd = os.getcwd()

app = os.path.join(cwd, "app.py")

print(app)

Output

/Users/krunal/Desktop/code/pyt/database/app.py

The cwd variable saves the file path for the database folder relative to our current working directory, and then we join with the app.py filename.

Conclusion

The os.path.join() method combines components in a pathname to create a full pathname. The os.path.join() method makes it easy to combine two or more pathname components. The os.path.join() automatically adds forward slashes (“/”) into the pathname when needed.

See also

Python os.listdir()

Python os.system()

Python os.path.commonprefix()

Python os.path.commonpath()

Python os.path.splitext()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.