- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Manipulating Pathnames using Python?
In this article, we will learn Manipulating Pathnames using Python.
Here are some different examples used mentioned below −
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
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task. −
Use the import keyword to import the os module.
Create a variable to store the input file path.
Use the basename() function(returns the base name from the given file path) of the os module to get the last component(main file name) of the input file path and print it.
Getting the main filename from the file path
Example
The following program returns the main file name from the input file using the os.path.basename() function −
# importing os module import os # input path of the file inputFilepath = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf' # Printing the given input path print("Give Input Path is:",inputFilepath) # getting the last component(main file name )of the input file path print("Base Name of the given path is :",os.path.basename(inputFilepath))
Output
On execution, the above program will generate the following output −
Give Input Path is: C:/Users/cirus/Desktop/tutorialsPoint.pdf Base Name of the given path is: tutorialsPoint.pdf
Getting the directory name from the file path
Use the os.path.dirname() function(returns the directory name from the given file path) to get the directory/folder of the given input file path by passing the input file path as an argument to it.
Example
The following program returns the directory name from the input file path using the os.path.dirname() function −
# importing os module import os # input path of the file inputFilepath = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf' # Printing the given input path print("Give Input Path is:",inputFilepath) # getting the directory/folder path from the input file path using dirname() function print("Directory path of the given path is: ",os.path.dirname(inputFilepath))
Output
On execution, the above program will generate the following output −
Give Input Path is: C:/Users/cirus/Desktop/tutorialsPoint.pdf Directory path of the given path is: C:/Users/cirus/Desktop
Joining path components together
os.path.join() function
Python's os.path.join() function effectively joins one or more path components. With the exception of the last path component, this approach concatenates different path components by placing exactly one directory separator ('/') after each non-empty portion. A directory separator ('/') is added at the end of the last path component to be joined is empty.
All previously joined components are deleted if a path component represents an absolute path, and joining moves on from the absolute path component.
Example
The following program joins the given path components with the base name using the os.path.join() function −
# importing os module import os # input path of the file inputFilepath = 'C:/Users/cirus/Desktop/kohli.pdf' # Printing the given input path print("Give Input Path is:",inputFilepath) # joining the components to the main file name of the input file path print("Joining the given paths to input Path:\n", os.path.join('tutorials', 'python', os.path.basename(inputFilepath)))
Output
On execution, the above program will generate the following output −
Give Input Path is: C:/Users/cirus/Desktop/kohli.pdf Joining the given paths to input Path: tutorials/python/kohli.pdf
Expanding the User's Home Directory
os.path.expanduser() function
The Python function os.path.expanduser() expands the initial path ~( tilde symbol) or ~ user in the specified path to the user's home directory.
Syntax
Below is the syntax of the function.
os.path.expanduser(path)
Example
The following program returns the expand path of the user’s home directory using the expanduser() function −
# importing os module import os # input path of the file inputFilepath = '~/Users/cirus' # Printing the given input path print("Give Input Path is:",inputFilepath) # Expanding the user's home directory print("Expanded Path is:\n",os.path.expanduser(inputFilepath))
Output
On execution, the above program will generate the following output −
Give Input Path is: ~/Users/cirus Expanded Path is: /root/Users/cirus
Splitting the file extension from file path
os.path.splitext() function − Splits the file path name into a pair root and extension. The root here is everything except the file extension.
If the given file path has no extensions, then the extension is empty. The given path will be ignored if it has a leading period ('.').
Syntax
Below is the syntax of the function.
os.path.splitext(path)
Use the os.path.splitext() function to split the file path and file extension from the input file path.
Example
The following program splits the main file path and file extension from the input file path using the os.path.splitext() function −
# importing os module import os # input path of the file inputFilepath ='C:/Users/cirus/Desktop/tutorialsPoint.pdf' # Printing the given input path print("Give Input Path is:",inputFilepath) # splitting the file path and file extension from the input file path # using the splitext() function print("Splitting the given path by extension:\n",os.path.splitext(inputFilepath))
Output
On execution, the above program will generate the following output −
Give Input Path is: C:/Users/cirus/Desktop/tutorialsPoint.pdf Splitting the given path by extension: ('C:/Users/cirus/Desktop/tutorialsPoint', '.pdf')
Conclusion
In this article, we learned how to use the OS module to modify the path names. From the file path, we learned how to extract the primary (base) filename and directory name. We learned how to combine the path's component names and paths. The user home directory expansion process was discussed. In the end, we figured out how to separate the file path from the extension.