Python os.path.join () method

| | | | | | | | | | | | | | | | | | | | | | | | | | |

The join method allows you to combine multiple paths using an assigned delimiter. For example, on Windows, the separator is a backslash (forward slash), but in Linux, the separator function is assigned to a forward slash. How it works:

import os
 
print(  os.path.join ((r’C:Python27Toolspynche’, ’ChipViewer.py’) )
# C:Python27ToolspyncheChipViewer.py

In this example, we have combined the directory and file paths together to get a working path. Note that the join method does not indicate the final result.

The operating system module in Python provides functions for interacting with the operating system. The operating system is part of the standard Python utility modules. This module provides a portable way to use OS dependent functionality. os.path module is a submodule of the OS module in Python used for common path manipulation.

The os.path.join ( () method in Python merges one or more path components intelligently. This method concatenates various path components with exactly one directory separator (’/’) following every non-empty part except the last path component. If the last component of the path to be merged is empty, a directory separator (’/’) is inserted at the end.

If a path component represents an absolute path, all previous joined components are deleted and the join continues from the absolute path component.

Syntax:  os.path.join ((path, *paths) 

Parameter: 

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. 
A path-like object is either a string or bytes object representing a path.

Note: The special syntax *args (here *paths) in function definitions in python is used to pass a variable number of arguments to a function. 

Return Type: This method returns a string which represents the concatenated path components. 

os.path.join

Code: Use of os.path.join (() method to join various path components

# Python program to explain  os.path.join (() method
   
# importing os module
import os
 
# Path
path = "/home"
 
# Join various path components
print( os.path.join ((path, "User/Desktop", "file.txt"))
 
 
# Path
path = "User/Documents"
 
# Join various path components
print( os.path.join ((path, "/home", "file.txt"))
 
# In above example ’/home’
# represents an absolute path
# so all previous components i.e User / Documents
# are thrown away and joining continues
# from the absolute path component i.e / home.
 
 
# Path
path = "/User"
 
# Join various path components
print( os.path.join ((path, "Downloads", "file.txt", "/home"))
 
# In above example ’/User’ and ’/home’
# both represents an absolute path
# but ’/home’ is the last value
# so all previous components before ’/home’
# will be discarded and joining will
# continue from ’/home’
 
# Path
path = "/home"
 
# Join various path components
print( os.path.join ((path, "User/Public/", "Documents", ""))
 
# In above example the last
# path component is empty
# so a directory separator (’/’)
# will be put at the end
# along with the concatenated value

Output:

/home/User/Desktop/file.txt
/home/file.txt
/home
/home/User/Public/Documents/
os.path.join

Combining path components

In the previous example, I deliberately used a slash "/" to separate path components. This is fine in principle, but not recommended. If you want your application to be cross-platform, this option is not suitable. For example, some older versions of Windows only recognize the slash "" as a separator.

But don’t worry, Python solves this problem perfectly thanks to the os.path.join ( () function. Let’s rewrite the example from the previous paragraph using this function:

os.path.exists ( os.path.join ( (’sample_data’, ’README.md’))

The join() function of the os.path module correctly joins the given path to one or more *paths components. The return value is the concatenation of path and the *paths components, with exactly one os.sep directory separator following every non-empty part except the last. This means that the result will only end with a delimiter if the last part is empty.

If the component is an absolute path, all previous components are discarded and the connection continues from the absolute path component.

On Windows, the drive letter is not reset when an absolute path component is encountered, such as r’foo’. If a component contains a drive letter, all previous components are discarded and the drive letter is reset. Because each drive has a current directory, os.path.join(’c:’, ’foo’) represents a path relative to the current directory on drive C: - c:foo, not c:\foo.

The path and *paths arguments must be of the same type and can be either byte strings or text strings. The result will be the same type.

The os.path.join() function can take an object representing a file system path, such as pathlib.PurePath.

 
>>> import os.path
>>> os.path.join(’home’, ’User’, ’Desktop’, ’file.txt’)
# ’home/User/Desktop/file.txt’

>>> os.path.join(’/home’, ’User/Desktop’, ’file.txt’)
# ’/home/User/Desktop/file.txt’

>>> os.path.join(’/home’, ’/User/Desktop’, ’file.txt’)
# ’/User/Desktop/file.txt’

>>> os.path.join(’User/Desktop’, ’/home’, ’file.txt’)
# ’/home/file.txt’
os.path.join

Python os.path.join on Windows

Windows has a concept of current directory for each drive. Because of that, "c:sourcedir" means "sourcedir" inside the current C: directory, and you’ll need to specify an absolute directory.

Any of these should work and give the same result, but I don’t have a Windows VM fired up at the moment to double check:

"c:/sourcedir"
os.path.join("/", "c:", "sourcedir")
os.path.join("c:/", "sourcedir")