Python os join path – 5 Examples

One of the most crucial features of the Python “os” module is the Python os join path, also known as “os.path.join.” The Python os join path, as the name suggests is used to join different paths together. In other words, 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. In this tutorial, we will learn how we can use the Python os join path method to join various paths together.

Introduction to the Python os join path

Python OS module provides the facility to establish the interaction between the user and the operating system. It offers many useful OS functions that are used to perform OS-based tasks and get related information about the operating system and let us work with the files and directories. One of the important methods is the Python os join path method. This function is utilized to concatenate two or more paths together into a single integrated path. However, an important thing to be understood here is that if you are going to provide an absolute path, i.e., a path starting with a forward slash “/” as an attribute to this function, then any attribute provided before this will be considered useless. Hence, it will be discarded. On the other hand, an attribute that will follow an absolute path will simply be concatenated with it. Moreover, if you will use an empty attribute ” ” as the last attribute to this function, then a backslash “\” will be introduced at the end of the concatenated path. Apart from that, this function can also be used with lists in Python. If this is confusing, no worries, we will understand it using various examples. But before it makes sure you have imported the os module.

# importing the os module
import os

Different methods available in the OS module

The OS module has a number of methods that can be used to do various tasks. Here, we’ll look at a few of the most widely used techniques. For instance, os.name(), os.mkdir(), os.getcwd(), and numerous other functions. The name of the operating system module that it imports is provided by the os.name() function. For example, see the following Python program.

# importing os module
import os  
# os.name method
print(os.name) 

Output:

posix

While os.mkdir() method is used to create a new directory as shown below:

# importing os module
import os   
# os.mkdir method
os.mkdir("Name of new folder") 

The above Python code will create a folder with the given name in the same directory.

Another useful method is os.getcwd(), with means, get the current working directory and it will return the full path of the current directory as shown below:

# importing os module
import os   
# os.getcwd method
print(os.getcwd())  

Output:

/home/student/Documents/Machine_Learning_Staff/Techfor-Today/learn python

Depending on your directory, the output will be different.

Python os join path method

Now, let us understand the python os join path method. The Python os join path method combines multiple names and returns a complete path. The following is the simple syntax of the Python os join path method.

# Python os path join method
os.path.join(path1, path2..., pathn)

The Python os join path method takes multiple arguments and returned a combined path.

python-os-join-path

For example, let us say that we have the following two paths and we want to merge them to create a new path.

# Importing os module
import os
# python os path join method
combined_path = os.path.join("/Users/machine_learning", "tutorial.py")
# printing the combined path
print(combined_path)

Output:

/Users/machine_learning/main_file.py

As you can see, we passed two arguments to the Python os join path method and it joins both together to return a new directory.

Python os join path with absolute path

We have already discussed for example how the Python os join path method combines different paths to give us one path. Now, we will learn how we can combine absolute paths with other paths. An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words, we can say the absolute path is a complete path from the start of the actual filesystem from the / directory. One important thing to understand here is that an important thing is that if we provide an absolute path, then any attribute provided before this will be considered useless because the absolute path starts from the root file so no need to provide any directory before it.

Example-1: Python os join path

Now let us jump into the practical part and join a path with an absolute path. As we said early that absolute path starts with ‘/’.

# defining absolute path
path = "/user"
# python os path join method
combined_path = os.path.join(path, "machine_learning", "tutorial.py")
# printing the combined path
print(combined_path)

Output:

/user/machine_learning/tutorial.py

As you can see, we have successfully concatenated the paths with an absolute path.

Example-2: Python os join Path

Now, let us try to pass the absolute path as the second argument using the same example.

# defining absolute path
path = "/user"
# python os path join method
combined_path = os.path.join("machine_learning",path, "tutorial.py")
# printing the combined path
print(combined_path)

Output:

/user/tutorial.py

As you can see, the path ‘mahine_learning’ was not added to the path because there shouldn’t be anything before the absolute path.

Example-3: Python os join path

Now, we will pass an empty string at the last of the path.

# defining absolute path
path = "/user"
# python os path join method
combined_path = os.path.join(path, "machine_learning", "tutorial.py", "")
# printing the combined path
print(combined_path)

Output:

/user/machine_learning/tutorial.py/

The only difference that this output has from our first example’s output is a backslash “\” is introduced at the end of the concatenated path which happened solely because of the introduction of the fourth empty attribute.

Example-4: Python os join path

This time we will create a list of paths and then will use the os join method to combine them together.

# defining absolute path
path = ["/user", "machine_learning", "tutorial.py"]
# python os path join method
combined_path = os.path.join(*path)
# printing the combined path
print(combined_path)

Output:

/user/machine_learning/tutorial.py

As you can see, we have used *args which allows us to pass a variable number of non-keyword arguments to a Python function.

Example-5: Python os join path

When we are using a list of directories, as we have seen in the above example, we have to be sure about the ordering. If we place an absolute path in between somewhere, then all paths before it will be ignored as shown below:

# defining absolute path
path = [ "machine_learning", "tutorial.py","/user"]
# python os path join method
combined_path = os.path.join(*path)
# printing the combined path
print(combined_path)

Output:

/user

As you can see, we only get the absolute path because all the other paths were in the list before it.

Summary

The os.path.join() method merges components in a pathname to create a full pathname. It automatically adds forward slashes (“/”) into the pathname when needed. In this short tutorial, we learned how we can use the python os join path method to join paths in Python using various examples.

Related Articles

1 thought on “Python os join path – 5 Examples”

  1. Pingback: ValueError: invalid literal for int() with base 10 | Solved - Techfor-Today

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top