Python

Python os.path.join Example

The “os.path.join” is a very important function of the “os” module of Python. 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 to 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. To grasp a sound understanding of all this, you will have to read all the examples we have provided in this article.

Examples of os.path.join in Python in Windows 10

Following are several relevant examples of using the “os.path.join” function in Python in Windows 10. These examples are, in fact, the different variants of this function through which you can get different outputs simply by tweaking them. You will be able to understand all this well by going through all these examples individually.

Example 1

In this example, we wanted to join an absolute path with a directory and a file present on our system. For that, we wrote the Python code shown in the image below:

# -*- coding: utf-8 -*-

"""
Created on Tues Sep 28 18:42:03 2021
@author: DELL
"""


import os
path = "/home"
print(os.path.join(path, "Desktop", "Project Proposal.docx"))

In this example, we imported the “os” module of Python first since the “os.path.join” function belongs to this module. After importing this module, we declared a variable named “path” and assigned an absolute path, such as the path of our Home directory. Then, we have a “print” command in which we have used the “os.path.join” function. We have assigned three attributes to this function. The first attribute was the “path” variable to which an absolute path was assigned earlier. The second attribute was a directory name, i.e., Desktop. In this case, the third attribute was the name of a file, i.e., Project Proposal.docx. With this code, we wanted to see how these different paths look after concatenating them with the help of this function.

The result of running this program is shown in the following image:

Since the first attribute that we provided to the “os.path.join” function was an absolute path, the other paths were simply concatenated with this path using the backslash “\“.

Example 2

In this example, again, we wanted to join an absolute path with a directory and a file present on our system, however, in a different order from the first example. For that, we wrote the Python code shown in the image below:

# -*- coding: utf-8 -*-

"""
Created on Tues Sep 28 18:42:03 2021
@author: DELL
"""


import os
path = "Desktop"
print(os.path.join(path, "/home", "Project Proposal.docx"))

In this example, after importing the “os” module, we declared a variable named “path” and assigned the path of a directory, i.e., Desktop. Then, we have a “print” command in which we have used the “os.path.join” function. We have assigned three attributes to this function. The first attribute was the “path” variable to which a directory path was assigned earlier. The second attribute was an absolute path, i.e., the path of our Home directory; in this case, the third attribute was the name of a file, i.e., Project Proposal.docx. With this code, we wanted to see how these different paths look after concatenating them with the help of this function.

The result of running this program is shown in the following image:

Now, since the second attribute of this function was an absolute path, everything before this attribute was discarded, and the concatenation took place after the absolute path.

Example 3

In this example, we wanted to join an absolute path with a directory and a file present on our system, however, in a different order as that of our first two examples. For that, we wrote the Python code shown in the image below:

# -*- coding: utf-8 -*-

"""
Created on Tues Sep 28 18:42:03 2021
@author: DELL
"""


import os
path = "Desktop"
print(os.path.join(path, "Project Proposal.docx", "/home"))

In this example, after importing the “os” module, we declared a variable named “path” and assigned to it the path of a directory, i.e., Desktop. Then, we have a “print” command in which we have used the “os.path.join” function. We have assigned three attributes to this function. The first attribute was the “path” variable to which a directory path was assigned earlier. The second attribute was the name of a file, i.e., Project Proposal.docx. Whereas the third attribute was an absolute path, i.e., the path of our Home directory. With this code, we wanted to see how these different paths look after concatenating them with the help of this function.

The result of running this program is shown in the following image:

Since the third attribute of this function contained an absolute path, therefore, everything before this attribute was discarded, and we were only left with this absolute path.

Example 4

This example is pretty much similar to our first example, with simply a slight difference that you will be able to figure out by looking at the Python code shown below:

# -*- coding: utf-8 -*-

"""
Created on Tues Sep 28 18:42:03 2021
@author: DELL
"""


import os
path = "/home"
print(os.path.join(path, "Desktop", "Project Proposal.docx", ""))

In this example, we have introduced only the fourth attribute to the “os.path.join” function to see how this attribute affects our output.

The result of running this program is shown in the following image:

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 that happened solely because of the introduction of the fourth empty attribute.

Example 5

This example is relatively different from our first four examples. In this one, we wish to use the “os.path.join” function with the list in Python. For that, we wrote the code shown in the image below:

# -*- coding: utf-8 -*-

"""
Created on Tues Sep 28 18:42:03 2021
@author: DELL
"""


import os

path_list = ["E:", "Path", "To", "File"]
file_path = os.path.join(*path_list)
print(file_path)

In this example, after importing the desired module, we have declared a list named “path_list” and have assigned to it a few strings. Then, we have a variable named “file_path”, to which we have assigned the result of the “os.path.join” function. The only attribute that we have passed to this function is a pointer to our list declared above. Finally, we have printed the value of the “file_path” variable.

The result of running this program is shown in the following image:

You can see from the output shown above how gracefully the “os.path.join” function has concatenated the paths provided to it by using a list in Python.

Conclusion

This article was all about using the “os.path.join” function of Python in Windows 10. We first talked briefly about the usage of this function, and after building a basic understanding, we shared five different examples with you that made use of this function in Python. These examples differed slightly from each other. However, by understanding these minor differences, you will be able to master the usage of this function of Python and will be able to use it quite effectively, as needed.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.