The Wayback Machine - https://web.archive.org/web/20240919101656/https://www.geeksforgeeks.org/python-os-path-normpath-method/
Open In App

Python | os.path.normpath() method

Last Updated : 16 Sep, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.

os.path.normpath() method in Python is used to normalize the specified path. All redundant separator and up-level references are collapsed in the process of path normalization. 

For example: A//B, A/B/, A/./B and A/foo/../B all will be normalized to A/B. 

On Windows operating system, any forward slash (‘/’) in the path is converted to backslash (‘\’).

Syntax: os.path.normpath(path)
Parameter: 
path: A path-like object representing a file system path. 
Return Type: This method returns a string value which represents the normalized path. 
 

Code #1: Use of os.path.normpath() method 

Python3




# Python program to explain os.path.normpath() method
   
# importing os.path module
import os.path
 
# Path
path = '/home//user/Documents'
 
 
# Normalize the specified path
# using os.path.normpath() method
norm_path = os.path.normpath(path)
 
# Print the normalized path 
print(norm_path)
 
# Path
path = '/home/./Documents'
 
 
# Normalize the specified path
# using os.path.normpath() method
norm_path = os.path.normpath(path)
 
# Print the normalized path 
print(norm_path)
 
# Path
path = '/home/user/temp/../Documents'
 
# Normalize the specified path
# using os.path.normpath() method
norm_path = os.path.normpath(path)
 
# Print the normalized path 
print(norm_path)


Output

/home/user/Documents
/home/Documents
/home/user/Documents

Code #2: Use of os.path.normpath() method (On Windows) 

Python3




# Python program to explain os.path.normpath() method
   
# importing os.path module
import os.path
 
# Path
path = r'C:/Users'
 
 
# Normalize the specified path
# using os.path.normpath() method
norm_path = os.path.normpath(path)
 
# Print the normalized path 
print(norm_path)
 
# Path
path = r'C:\Users\.\Documents'
 
 
# Normalize the specified path
# using os.path.normpath() method
norm_path = os.path.normpath(path)
 
# Print the normalized path 
print(norm_path)
 
# Path
path = r'C:\Users\admin\temp\..\Documents'
 
# Normalize the specified path
# using os.path.normpath() method
norm_path = os.path.normpath(path)
 
# Print the normalized path 
print(norm_path)


Output: 

C:\\Users
C:\\Users\\Documents
C:\\Users\\admin\\Documents

 

Reference: https://docs.python.org/3/library/os.path.html
 



Previous Article
Next Article

Similar Reads

Python | os.path.splitdrive() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is a submodule of OS module in Python used for common pathname manipulation. os.path.splitdrive() method in Python i
3 min read
Python | os.path.getctime() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.getctime() method in Python is
2 min read
Python | os.path.getatime() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.getatime() method in Python is
2 min read
Python | os.path.getmtime() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.os.path.getmtime() method in Python is u
2 min read
Python | os.path.isfile() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.isfile() method in Python is us
1 min read
Python | os.path.commonprefix() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common path name manipulation. os.path.commonprefix() method in Python
2 min read
Python | os.path.commonpath() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common path name manipulation. os.path.commonpath() method in Python is
2 min read
Python | os.path.splitext() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common pathname manipulation. os.path.splitext() method in Python is us
2 min read
Python | os.path.expandvars() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path the module is a submodule of OS module in Python used for common pathname manipulation. os.path.expandvars() method in Pyth
3 min read
Python | os.path.islink() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.islink() method in Python is us
2 min read
Python | os.path.lexists() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in python used for common path name manipulation. os.path.lexists() method in Python is u
2 min read
Python | os.path.samefile() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.samefile() method in Python is
2 min read
Python | os.path.sameopenfile() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.os.path.sameopenfile() method in Python
3 min read
Python | os.path.samestat() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.samestat() method in Python is
2 min read
Python | os.path.realpath() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.realpath() method in Python is
2 min read
Python | os.path.relpath() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.relpath() method in Python is u
3 min read
Python | os.path.expanduser() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common pathname manipulation.os.path.expanduser() method in Python is
4 min read
Python PIL | ImagePath.Path.tolist() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImagePath module is used to store and manipulate 2-dimensional vector data. Path objects can be passed to the methods on the ImageDraw module. ImagePath.Path.tolist() Converts the path to a Python list [(x, y), â€Ķ]. Syntax: PIL.ImagePath.Path
1 min read
Python PIL | ImagePath.Path.map() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImagePath module is used to store and manipulate 2-dimensional vector data. Path objects can be passed to the methods on the ImageDraw module. ImagePath.Path.map() maps the path through the function. Syntax: ImagePath.Path.map(function) Para
1 min read
Python PIL | ImagePath.Path.getbbox() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImagePath module is used to store and manipulate 2-dimensional vector data. Path objects can be passed to the methods on the ImageDraw module. ImagePath.Path.getbbox() Gets the bounding box of the path. Syntax: ImagePath.Path.getbbox() Param
1 min read
Python | os.path.normcase() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.os.path.normcase() method in Python is u
2 min read
Python | os.path.dirname() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.dirname() method in Python is u
2 min read
Python | os.path.isabs() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.isabs() method in Python is use
2 min read
Python | os.path.split() method
os.path.split() method in Python is used to Split the path name into a pair, and tail. Here, the tail is the last path name component and the head is everything leading up to that. For example, consider the following path names: path name = '/home/User/Desktop/file.txt' In the above example, the ‘file.txt’ component of the path name is tail, and ‘/
3 min read
Python | os.path.size() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependentthe functionality. os.path module is a submodule of the OS module in Python used for common path name manipulation. In this article, we will discus
3 min read
Python | os.path.exists() method
os.path.exists() method in Python is used to check whether the specified path exists or not. This method can be also used to check whether the given path refers to an open file descriptor or not. os.path.exists() Syntax in PythonSyntax: os.path.exists(path) Parameter: path: A path-like object representing a file system path. A path-like object is e
2 min read
Python | os.path.ismount() method
os.path module is sub module of Python OS Module in Python used for common path name manipulation. os.path.ismount() method in Python is used to check whether the given path is a mount point or not. A mount point is a point in a file system where a different file system has been mounted. os.path.ismount() Syntax in PythonSyntax: os.path.ismount(pat
2 min read
Python | os.path.isdir() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependent functionality. os.path module is a sub-module of the OS module in Python used for common path name manipulation. os.path.isdir() method in Python
2 min read
Python | os.path.join() method
Os Path Module is a sub-module of the OS module in Python used for common pathname manipulation. In this article, we will learn about os.path.join() and handling file paths safely in Python OS. Python os.path.join() Method SyntaxSyntax: os.path.join(path, *paths) Parameter: path: A path-like object representing a file system path. *path: A path-lik
4 min read
Python | os.path.basename() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.os.path.basename() method in Python is u
4 min read
Practice Tags :