os.path.join() is a method of os module in Python that can combine file or directory path names as a single path. This method is often used along with the other os modules method where we need to perform a joining path operation. The different operating system has different path system, so instead of writing the path structure manually, we use the os module path handling methods such as
and do the task automatically.
How to Use os.path.join() method in Python?
Often working with files and directories in Python we come across the situation where we want to write a script that joins the file name with an absolute directory path. Although we can use the string concatenation operation to write the complete pathname, still when we want our program to run the same on any file path system, we use the inbuilt module like
which is compatible with all the popular operating systems that support Python.
This Python guide will walk you through the os.path.join function that can combine a directory path with a file or directory name.
What is a File Path?
A file or directory path is a valid sequence of file or directory names that are located in a system. Different operating systems use different types of file systems, and to represent those file systems, they use different file path notations. For instance, the Windows operating system uses the backward slash "\" to represent the sequence of the file path.
And operating systems, like Mac and Linux, use the forward-slash "/" for file or directory paths.
What is Python os.path.join?
os is an inbuilt Python module that provides operating system functionalities to Python. As file management is a part of the operating system we can also use the
module to handle files using Python. With os.path.join function, we can combine the two or more pathnames to create a single file pathname. The path.join function does not create any new path instead, it just provides a name of the path by combining the specified path names.
import os os.path.join(path1, path2)
method will join
as a single pathname.
import os path1= r"C:\Users\admin\code" #directory path path2 = r"index.html" #file name full_path = os.path.join(path1, path2) print("The Full Path is:", full_path)Output
The Full Path is: C:\Users\admin\code\index.html
In the above example, I have represented
as a raw string, by putting the prefix
. This is because I am on a windows system, and windows use backslash "\" as a directory separator for the file system.
And in Python string, we use backslash as an escape character. And if we write the Windows file path as it is, the string will treat the directory separator backslash as an escape character and raise an error (SyntaxError: (unicode error) 'unicodeescape'), when the
method will try to join the path names. To represent the windows file path as a string, we should always consider using a raw string.
Real use case of os.path.join() method
In the above example, we hardcoded the name for both paths
. Using the path.join method in such a case does not make any sense. We could have also used string concatenation and joined both the pathname. The os.path.join() method really shines when it is used along with other os methods where we are getting the pathname dynamically, and we wish to name the complete path.
Let's create a Python program that prints the absolute pathname for all the files and directories present in the current directory.
import os # get the path for current directory current_dir = os.getcwd() # list the name of all the files and diretories all_files = os.listdir() for name in all_files: # join the current directory path with file name print(os.path.join(current_dir, name))
D:\code\main.py D:\code\mousesimulate.py D:\code\movies.csv D:\code\msedgedriver.exe D:\code\music.mp3 D:\code\my.pdf
In the above example, first, we get the directory pathname for our current directory using the
method. Then we get the list of all the files that are present in our current directory using
method. The os.listdir() method returns a list of all file and directory names present in the current directory. At last, we loop throw the
list and combine the name of all the files or directories present in the list with the current directory path using
The os.path.join function combines the name of two paths and return a string value of the complete pathname. With this method, we can easily combine two paths and create an absolute pathname. This method automatically adds a forward or backward slash between the specified path based on the operating system where the python program is running. This method come very useful when we are working with file management in python and want to list the complete or absolute path for the files or directories.
People are also reading:
- HOG Feature Extraction in Python
- Python copy file and directory using shutil
- Install Python Package Using Jupyter Notebook
- Python Coding on MacBook
- Extract all Stored Chrome Password with Python
- How to delete emails in Python?
- Python readline() Method with Examples
- Install python package using jupyter Notebook
- Automate Login in Python
- Python map() function with Examples
Leave a Comment on this Post