How to manipulate pathnames with Python

How to Manipulate Pathnames in Python

In this article, we will learn how to manipulate pathnames in Python.

Some different examples are mentioned below

  • Getting the main file name from a file path

  • Getting the directory name from a file path

  • Concatenating path components

  • Extending the user’s home directory

  • Stripping the file extension from a file path

Algorithm (Steps)

Below are the algorithm/steps you need to follow 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 os module’s basename() function (which returns the base name from a given file path) to get the last component of the input file path (the main file name) and print it.

Getting the Main File Name from a File Path

Example

The following program uses the os.path.basename() function to return the main file name from an input file.

# 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

When executed, the above program will produce the following output –

Give Input Path is: C:/Users/cirus/Desktop/tutorialsPoint.pdf
Base Name of the given path is: tutorialsPoint.pdf

Getting a Directory Name from a File Path

Use the os.path.dirname() function (which returns the directory name from a given file path) to get the directory/folder for a given input file path by passing it the input file path as an argument.

Example

The following program uses the os.path.dirname() function to return a directory name from an input file path.

# 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

When executed, the above program will produce the following output –

Give Input Path is: C:/Users/cirus/Desktop/tutorialsPoint.pdf
Directory path of the given path is: C:/Users/cirus/Desktop

<h2>Joining Path Components Together</h2>
<h3>os.path.join() Function</h3>
<p>Python's os.path.join() function effectively concatenates one or more path components. This method concatenates different path components by placing exactly one directory separator ('/') after each non-empty component, except the last. A directory separator ('/') is added to the end of the last concatenated path component that is empty.</p>
<p>If a path component represents an absolute path, all previously joined components are removed, and joining continues from the absolute path component.</p>
<h3>Example</h3>
<p>The following program uses the os.path.join() function to concatenate the given path components with the base name. </p>
<pre><code class="language-python line-numbers"># 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

When executed, the above program will produce the following output:

Give Input Path is: C:/Users/cirus/Desktop/kohli.pdf
Joining the given paths to the input Path:
tutorials/python/kohli.pdf

Expanding the User’s Home Directory

os.path.expanduser() Function

Python Function os.path.expanduser() Expands the initial pathname ~ (tilde symbol) or ~user in the specified path to the user’s home directory.

Syntax

The following is the syntax of this function.

os.path.expanduser(path)

Example

The following program uses the expanduser() function to return the expanded pathname of the user’s home directory.

# importing the 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

When executed, the above program will produce the following output:

Give Input Path is: ~/Users/cirus
Expanded Path is:
/root/Users/cirus

Splitting File Extensions from a File Path

os.path.splitext() Function – Splits a file path name into a root and extension pair. Here, the root is everything except the file extension.

If the given file path has no extension, the extension is empty. If the given path has a leading period (‘.’), it is ignored.

Syntax

Below is the syntax of this 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 uses the os.path.splitext() function to split the main file path and file extension from the input file path.

# 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

When executed, the above program will produce 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')

Summary

In this article, we learned how to use operating system modules to modify path names. We learned how to extract the primary (base) file name and directory name from a file path. We learned how to combine the path component names with the path. We discussed the expansion process for user home directories. Finally, we figured out how to separate the file path from the extension.

Leave a Reply

Your email address will not be published. Required fields are marked *