Python| os.path.normcase() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。 os.path模块是Python中OS 模块的子模块,用于常见的路径名操作。
Python中的os.path.normcase()方法用于规范指定路径名的大小写。在 Windows 上,此方法将指定路径中的所有字符转换为小写,并将正斜杠 ('/') 转换为反斜杠 ('\')。此方法在 Windows 以外的操作系统上返回未更改的指定路径。
Syntax: os.path.normcase(path)
Parameter:
path: A path-like object representing a file system path.
Return Type: This method returns a string value which represents the normalized case in the specified path.
代码 #1:使用os.path.normcase()方法(在 Windows 上)
Python3
# 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)
Python3
# 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
输出:
c:\\user\\admin\\documents
\\home\\user
c:\\users\\desktop
代码 #2:使用os.path.normcase()方法(在 Windows 以外的操作系统上)
Python3
# 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/
参考: https://docs。 Python.org/3/library/os.path.html