>>> path1 = "c:/fold1/fold2"
>>> list_of_paths = ["c:\\fold1\\fold2","c:\\temp\\temp123"]
>>> os.path.normpath(path1)
'c:\\fold1\\fold2'
>>> os.path.normpath(path1) in list_of_paths
True
>>> os.path.normpath(path1) in (os.path.normpath(p) for p in list_of_paths)
True
Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path (if they are supported by the operating system).
New in version 2.2.
那么:
if os.path.realpath(path1) in (os.path.realpath(p) for p in list_of_paths):
# ...
使用^{} 将
c:/fold1/fold2
转换为c:\fold1\fold2
:在Windows上,必须使用^{} 比较路径,因为在Windows上,路径不区分大小写。
^{} 模块包含几个函数来规范化文件路径,以便等效路径规范化为同一字符串。您可能需要
normpath
、normcase
、abspath
、samefile
或其他工具。所有这些答案都提到} :
os.path.normpath
,但没有一个提到^{那么:
相关问题 更多 >
编程相关推荐