Python os.path.normcase() Method

Musfirah Waseem Jan 30, 2023
  1. Syntax of Python os.path.normcase() Method
  2. Example Codes: Use the os.path.normcase() Method on Windows OS
  3. Example Codes: Use the os.path.normcase() Method on OS Other Than the Windows OS
Python os.path.normcase() Method

Python’s os.path.normcase() method is the most efficient way of normalizing the case of a specified path. On Windows OS, path normalization includes converting all characters to the lowercase and any forward slash (/) into a backslash (\).

Syntax of Python os.path.normcase() Method

os.path.normcase(path address)

Parameters

path address This is the address object of a file system path or a symlink. This object can either be an str or bytes.

Return

This method returns a string value, which is a path string containing the normalized case of a file.

Example Codes: Use the os.path.normcase() Method on Windows OS

import os

path = "/home///user/Documents"

normalize = os.path.normcase(path)

print(normalize)

path = "/home/./Documents"

normalize = os.path.normcase(path)

print(normalize)

path = "/home/user/temp/../Documents"

normalize = os.path.normcase(path)

print(normalize)

Output:

\home\\\user\documents
\home\.\documents
\home\user\temp\..\documents

Note that on Windows OS, theos.path.normcase() method converts /A/BB/C to \a\bb\c.

Example Codes: Use the os.path.normcase() Method on OS Other Than the Windows OS

import os

path = "/home///user/Documents"

normalize = os.path.normcase(path)

print(normalize)

path = "/home/./Documents"

normalize = os.path.normcase(path)

print(normalize)

path = "/home/user/temp/../Documents"

normalize = os.path.normcase(path)

print(normalize)

Output:

/home///user/Documents
/home/./Documents
/home/user/temp/../Documents

This method returns an unchanged path address on another operating system, excluding the Windows OS.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS