Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Manipulating Pathnames using Python?
In this article, we will learn how to manipulate pathnames using Python's os.path module. Python provides several built-in functions to work with file paths, making it easy to extract components, join paths, and handle different operating systems.
We will explore the following pathname manipulation techniques ?
Getting the main filename from the file path
Getting the directory name from the file path
Joining path components together
Expanding the user's home directory
Splitting the file extension from the file path
Getting the Main Filename
The os.path.basename() function returns the last component (filename) from a given file path ?
import os
# Input path of the file
file_path = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf'
# Getting the base filename from the path
filename = os.path.basename(file_path)
print("File path:", file_path)
print("Base filename:", filename)
File path: C:/Users/cirus/Desktop/tutorialsPoint.pdf Base filename: tutorialsPoint.pdf
Getting the Directory Name
The os.path.dirname() function returns the directory portion of a file path ?
import os
# Input path of the file
file_path = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf'
# Getting the directory path
directory = os.path.dirname(file_path)
print("File path:", file_path)
print("Directory path:", directory)
File path: C:/Users/cirus/Desktop/tutorialsPoint.pdf Directory path: C:/Users/cirus/Desktop
Joining Path Components
The os.path.join() function combines multiple path components using the correct separator for your operating system ?
import os
# Joining path components
joined_path = os.path.join('tutorials', 'python', 'examples', 'demo.py')
print("Joined path:", joined_path)
# Joining with existing filename
file_path = 'C:/Users/cirus/Desktop/document.pdf'
filename = os.path.basename(file_path)
new_path = os.path.join('backup', 'files', filename)
print("New path:", new_path)
Joined path: tutorials/python/examples/demo.py New path: backup/files/document.pdf
Expanding User's Home Directory
The os.path.expanduser() function expands the tilde (~) symbol to the user's home directory path ?
import os
# Path with tilde symbol
tilde_path = '~/Documents/myfile.txt'
expanded_path = os.path.expanduser(tilde_path)
print("Original path:", tilde_path)
print("Expanded path:", expanded_path)
# Another example
config_path = os.path.expanduser('~/.config/app.conf')
print("Config path:", config_path)
Original path: ~/Documents/myfile.txt Expanded path: /root/Documents/myfile.txt Config path: /root/.config/app.conf
Splitting File Extension
The os.path.splitext() function splits a path into root and extension components ?
import os
# Splitting file extension
file_path = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf'
root, extension = os.path.splitext(file_path)
print("File path:", file_path)
print("Root:", root)
print("Extension:", extension)
# Example with multiple extensions
archive_path = '/backup/data.tar.gz'
root2, ext2 = os.path.splitext(archive_path)
print("\nArchive path:", archive_path)
print("Root:", root2)
print("Extension:", ext2)
File path: C:/Users/cirus/Desktop/tutorialsPoint.pdf Root: C:/Users/cirus/Desktop/tutorialsPoint Extension: .pdf Archive path: /backup/data.tar.gz Root: /backup/data.tar Extension: .gz
Common Operations Summary
| Function | Purpose | Example Input | Example Output |
|---|---|---|---|
basename() |
Get filename | /home/user/file.txt | file.txt |
dirname() |
Get directory | /home/user/file.txt | /home/user |
join() |
Combine paths | ('home', 'user', 'file.txt') | home/user/file.txt |
expanduser() |
Expand ~ | ~/Documents | /home/user/Documents |
splitext() |
Split extension | file.txt | ('file', '.txt') |
Conclusion
Python's os.path module provides essential functions for pathname manipulation. Use basename() and dirname() to extract path components, join() for cross-platform path construction, and splitext() for handling file extensions effectively.
