Python os.path.join() method [Practical Examples]

 

Introduction to Python os.path.join Method

The OS module in Python provides functions for interacting with the operating system. OS comes under Python's standard utility modules. This module provides a portable way of using operating system dependent functionality. One of its main properties is to help us to interact with file paths and directories. In this tutorial, we will learn about python os.path.join. We will learn what is OS module is and what is a file in Python, then we will cover the basic syntax of the python os.path.join method by taking examples.

We will cover different examples, by providing different arguments to the Python os.path.join method. Moreover, we will also discuss how we can join a list of file paths using the join method. In a nutshell, this tutorial contains all the details and information that you need to know in order to start working with the Python os.path.join method.

Advertisement

 

Getting started with Python os.path.join method

As we already discussed that the 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. We need to first import the OS module to get access to its various functionalities. We can use the import keyword to import the module.

import os

In the following section, we will discuss the os module in more detail and will cover some of the important methods that are available in the OS module.

 

Methods available in the OS module

There are various methods that are available in the OS module which perform different functions. Here we will have a look at some of the popular methods. For example, os.name(), os.mkdir(), os.getcwd(),  and many more. The os.name()function provides the name of the operating system module that it imports. For example, see the python program below:

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

Output:

posix

Similarly, theos.mkdir()function is used to create a new directory. Consider the following example.

# importing os module
import os   
# os.mkdir method
os.mkdir("location of new directory") 

The above program will create a new directory to the path in the string argument of the function.  The os.getcwd() method returns the name of the current directory. See the Python example below:

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

Output:

/home/uca/Downloads/python

Depending on the directory, you are working on, the output will be different for you.

 

Python os.path.join method

The Python os.path.join method combines path names into one complete path. This means that we can merge multiple parts of a path into one using the os.path.join method instead of hard-coding every pathname manually. The simple syntax of the Python path join method looks like this:

# importing os module
import os
# Python os path join method
os.path.join(path1, path2...)

This methods take as many arguments as you passed, and will return a combined path by merging those arguments. For example, see the example below:

# Importing os module
import os
# python os path join method
combined_path = os.path.join("/Users/Bashir/Python/tutorials", "main_file.py")
# printing the combined path
print(combined_path)

Output:

/Users/Bashir/Python/tutorials/main_file.py

Notice that we had passed two arguments to the python os.path.join method. First was the directory of the folder and then the file name and then the join method combine those together and returned a full path.

 

Examples of Python os.path.join method with absolute path

As we already discussed that the os.path.join method 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 we provide an absolute path, (a path starting with a forward slash "/" as an attribute to the function) then any attribute provided before this will be considered useless.

On the other hand, an attribute that will follow an absolute path will simply be concatenated to it. Moreover, if we use an empty attribute ("") as the last attribute to the os.path.join method, then a backslash "\" will be introduced at the end of the concatenated path. In this section, we will have a look at all these different scenarios and will solve various examples to understand the os.path.join method with absolute path in more detail.

Advertisement

 

Example-1:

Now let us take an example and join an absolute path with a directory and a file present on our system. See the example below:

# Importing os module
import os
# absolute path
path = "/Home"
# python os path join method
combined_path = os.path.join(path, "tutorials", "main_file.py")
# printing the combined path
print(combined_path)

Output:

/Home/tutorials/main_file.py

In this example, we imported the OS module of Python first since the os.path.join function belongs to this module. Then we declared a variable named path and assigned an absolute path, such as the path of our Home directory. Then, we have used the path join method to combine the absolute path with the other provided paths. The above code prints the combined path. Because the first path was an absolute path, the rest all were concatenated with it.

 

Example-2:

Now let us take an example of joining an absolute path with a directory and file as we did in the previous example but in a different order. This time we will provide the absolute path as a second argument. See the example below:

# Importing os module
import os
# absolute path
path = "/Home"
# python os path join method
combined_path = os.path.join("tutorials", path, "main_file.py")
# printing the combined path
print(combined_path)

Output:

/Home/main_file.py

Notice that the directory which was passed as the first argument was not concatenated because any directory before the absolute path will be ignored. 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:

Let us take another example and provide the absolute path as the last argument to the Python os.path.join method and see what will happen. See the example below:

# Importing os module
import os
# absolute path
path = "/Home"
# python os path join method
combined_path = os.path.join("tutorials", "main_file.py", path)
# printing the combined path
print(combined_path)

Output:

Advertisement
/Home

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:

Now let us take an example and pass an empty string as an argument to the python os.path.join method and see what will happen. See the example below:

# Importing os module
import os
# absolute path
path = "/Home"
# python os path join method
combined_path = os.path.join(path, "tutorials", "main_file.py", "")
# printing the combined path
print(combined_path)

Output:

/Home/tutorials/main_file.py/

The only difference that this output has from the example’s output is a forward slash ''/' is introduced at the end of the concatenated path that happened solely because of the introduction of the fourth empty attribute.

 

Example-5:

Now let us take an example and provide all empty strings as arguments to the Python os.path.join method and see what will happen. See the example below:

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

Output:


This will return nothing as we are not providing any argument.

 

Example-6:

Now let us use the Python os.path.join method with the Python list. We will list the directories in a list and will combine them together using the os.path.join method. See the example below:

# Importing os module
import os
# list
path = ["home", "tutorial", "main.py"]
# python os path join method
combined_path = os.path.join(*path)
# printing the combined path
print(combined_path)

Output:

home/tutorial/main.py

The only attribute that we have passed to the above function is a pointer to our list declared. The path join method takes the elements of the list and combines them together.

 

Python os.path.join method to list the directories

Let’s use the os.path.join method to return the full file paths of all the files in a folder. We will first create a list of all the files that are in our current directory and then will print them. See the example below:

# Importing OS module
import os
# geting the current directory
cwd = os.getcwd()
# joining the current directory
path = os.path.join(cwd)
# creating list of path
files = os.listdir(path)
# using for loop
for file in files:
    # printing the file name
	print(os.path.join(path, file))

Output:

/home/uca/Downloads/python/Python.py
/home/uca/Downloads/python/File
/home/uca/Downloads/python/main.py

Depending on your current directory, you may get a different list of files.

 

Summary

The OS module in Python provides functions for creating and removing a directory (folder), fetching its contents, changing and identifying the current directory, etc. It has many built-in functions that are used to perform different functions. In this tutorial, we learned about the Python os.path.join method. We learned how it works in different scenarios by solving various examples. Moreover, we also learned how we can list the list of files with the whole path by using the os.path.join method.

To summarize, this tutorial contains all the necessary details and information that you need to know in order to start working with the Python os.path.join method.

 

Further Reading

Python OS module
Python OS path method
Python OS methods

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment