Python os.path.normcase() Method

Python os.path.normcase() method is used to normalize the case of a pathname. This method can be beneficial for ensuring consistent path comparisons.

Syntax

os.path.normcase(path)

Parameters

Name Description
path (string) It is a path you want to normalize.

Return value

It returns a new string representing the normalized pathname.

Example 1: Normalizing Path on Unix/Linux

Visual Representation of Python os.path.normcase() Method

On Unix-like systems, which are case-sensitive, this method returns the path as is without any modification.

import os

path = "/Users/krunal/Desktop/code/pyt/app.py"

normalized_path = os.path.normcase(path)

print(normalized_path)

Output

/Users/krunal/Desktop/code/pyt/app.py

Example 2: Normalizing Path on Windows

Normalizing Path on Windows

Windows primarily uses backslashes (\) as the path separator. This method converts forward slashes (/) in the path to backslashes.

import os

path = "C:\\User\\Desktop\\new.txt"

normalized_path = os.path.normcase(path)

print(normalized_path)

Output

C:\User\Desktop\new.txt

Example 3: Cross-Platform Path Normalization

import os
import sys

path = 'C:\\User\\Desktop\\new.txt' if sys.platform == 'win32' else '/home/user/Desktop/new.txt'

normalized_path = os.path.normcase(path)

print(normalized_path)

Output

/home/user/Desktop/new.txt

That’s all!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.