How to manipulate pathnames using Python?
In this article, we will learn to manipulate pathnames using Python.
Here are some different examples mentioned below -
Get the main file name from the file path
Get directory name from file path
Connect path components together
Expand the user's home directory
Separate file extension from file path
Algorithm (steps)
Below are the algorithms/steps that need to be followed to perform the required task. -
Use the import keyword to import the os module.
Create a variable to store the input file path.
Use the basename() function of the os module (which returns the base name of the given file path) to get the last component of the input file path (the main file name) and print it out.
Get the main file name from the file path
Example
The following program uses the os.path.basename() function to return the main file name from the input file -
1 2 3 4 5 6 7 8 9 10 11 |
|
Output
When executed, the above program will generate the following output -
1 2 |
|
Get directory name from file path
Use the os.path.dirname() function (which returns the directory name from the given file path) to get the directory/folder of the given input file path by passing it as an argument.
Example
The following program uses the os.path.dirname() function to return the directory name from the input file path -
1 2 3 4 5 6 7 8 9 10 |
|
Output
When executed, the above program will generate the following output -
1 2 |
|
Connect path components together
os.path.join() function
Python’s os.path.join() function effectively joins one or more path components. This method joins different path components by placing a directory separator ('/') after each non-empty portion except the last. When the last path component to be added is empty, add a directory separator ("/") at the end.
If the path component represents an absolute path, all previously connected components will be removed and the connection will continue starting from the absolute path component.
Example
The following program uses the os.path.join() function to join the given path components with the base name -
1 2 3 4 5 6 7 8 9 10 11 |
|
Output
When executed, the above program will generate the following output -
1 2 3 |
|
Expand the user's home directory
os.path.expanduser() function
Python function os.path.expanduser() Expands the initial path ~ (tilde) or ~user in the specified path to the user's home directory.
grammar
The following is the syntax of this function.
1 |
|
Example
The following program uses the expanduser() function to return the expanded path of the user's home directory -
1 2 3 4 5 6 7 8 9 |
|
Output
When executed, the above program will generate the following output -
1 2 3 |
|
Separate file extension from file path
os.path.splitext() function - Splits a file pathname into a pair of root and extension. The root here is everything except the file extension.
If the given file path has no extension, the extension will be empty. If a given path has a leading period ("."), the path is ignored.
grammar
The following is the syntax of this function.
1 |
|
Use the os.path.splitext() function to split the file path and file extension from the input file path.
Example
The following program uses the os.path.splitext() function to split the main file path and file extension from the input file path -
1 2 3 4 5 6 7 8 9 10 |
|
Output
When executed, the above program will generate the following output -
1 2 3 |
|
in conclusion
In this article, we learned how to use OS modules to modify pathnames. From the file path, we learned how to extract the main (base) file name and directory name. We learned how to combine the component name of a path with the path. The user home directory expansion process is discussed. Finally, we found out how to separate the file path from the extension.
The above is the detailed content of How to manipulate pathnames using Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
