Python os.path.normcase()
Python os.path.normcase() 方法在Python中用于规范化指定路径名的情况。在windows上,此方法将指定路径中的所有字符转换为小写,并将正斜杠(‘ / ‘)转换为反斜杠(‘ \ ‘)。此方法返回在Windows以外的操作系统上未更改的指定路径。
语法: os.path.normcase(path)
参数:
path:表示文件系统路径的类路径对象。
返回类型: 此方法返回一个字符串值,该值表示指定路径中的规范化情况。
示例1
使用 os.path.normcase() 方法(在Windows上)
# Python program to explain os.path.normcase() method
# importing os.path module
import os.path
# Path
path = r'C:\User\admin\Documents'
# Normalize the case of
# characters in the specified path
norm_path = os.path.normcase(path)
# Print the normalized path
print(norm_path)
# Path
path = '/hoMe/UseR/'
# Normalize the case of
# characters in the specified path
norm_path = os.path.normcase(path)
# Print the normalized path
print(norm_path)
# Path
path = r'C:\Users/Desktop'
# Normalize the case of
# characters in the specified path
norm_path = os.path.normcase(path)
# Print the normalized path
print(norm_path)
输出:
c:\\user\\admin\\documents
\\home\\user
c:\\users\\desktop
示例2
使用 os.path.normcase() 方法(适用于Windows以外的操作系统)
# Python program to explain os.path.normcase() method
# importing os.path module
import os.path
# Path
path = '/home/UseR/Documents'
# Normalize the case of
# characters in the specified path
norm_path = os.path.normcase(path)
# Print the normalized path
print(norm_path)
# Path
path = '/hoMe/UseR/'
# Normalize the case of
# characters in the specified path
norm_path = os.path.normcase(path)
# Print the normalized path
print(norm_path)
# os.path.norcase() method will return
# the specified path as it as
# on operating systems
# other than Windows
输出:
/home/UseR/Documents
/hoMe/UseR/