Python os.path.normpath() Method



The normpath(path) method in the os.path module is used to get the normalized pathname of the provided pathname. Path normalization involves simplifying a pathname by removing redundant components such as extra separators (//), references to the current directory (./), and references to the parent directory (../). This method ensures that paths are represented in a consistent and valid format across different platforms.

On Windows operating systems, this method converts forward slashes to backward slashes, thus this normalization may change the meaning of a path containing symbolic links.

Syntax

Following is the syntax of the method −

os.path.normpath(path)

Parameters

Here are the details of its parameters −

  • path: This parameter represents a path-like object, which can be either a string or bytes object representing a file system path, or an object implementing the 'os.PathLike' protocol.

Return Value

The method returns a string representing the normalized version of the provided path.

Examples

Let's explore some examples to understand how the os.path.normpath() method works.

Example

The following example uses the os.path.normpath() method to get the normalized version of the input path 'A//B/C/./../D'.

# Import the os module
import os 

# Normalizing a path
path = 'A//B/C/./../D'
normalized_path = os.path.normpath(path)

# Print the results 
print(normalized_path)
# Verify the type of the normpath output
print('Type of the normpath output:',type(normalized_path))

Output

On executing the above code you will get the following output −

A/B/D
Type of the normpath output: class 'str'>
If you execute the above program in the Windows operating systems you will get the below output. Forward slashes or converted to backward slashes.
A\B\D
Type of the normpath output: class 'str'>

Example

In this example, the os.path.normpath() method to get normalized path from the input path which contains the symbolic links "..", usually this refers to the parent directory.

# Import the os module
import os 

# Normalizing a path
path = 'A/foo/../B'
normalized_path = os.path.normpath(path)

# Print the results 
print('The resultant normalized path:',normalized_path)

Output

On executing the above code in our online compiler will get the following output −

The resultant normalized path: A/B

Example

In this example the path is provided to the os.path.normpath() method with the "." as a path segment to get the normalized path.

# Import the os module
import os 

# Normalizing a path
path = 'mydir/./myfile.txt'
normalized_path = os.path.normpath(path)

# Print the results 
print('The resultant normalized path:',normalized_path)

Output

On executing the above code in our online compiler will get the following output −

The resultant normalized path: mydir/myfile.txt

Example

Here is the another example that uses the os.path.normpath() method to normilize the following pathname "D://a//b//c/../e/./myfile.txt".

# Import the os module
import os 

# Normalizing a path
path = 'D://a//b//c/../e/./myfile.txt'
normalized_path = os.path.normpath(path)

# Print the results 
print('The resultant normalized path:',normalized_path)

Output

On executing the above code in our online compiler will get the following output −

The resultant normalized path: D:/a/b/e/myfile.txt
os_path_methods.htm
Advertisements