我想知道是否可以访问linux路径,如:/home/dan/CaseSensitivE/test.txt。
在某种程度上,我们将其编写为/home/dan/casesensitive/test.txt,并将其写入正确的位置,这意味着python认为路径不区分大小写,并允许以这种方式输入路径,尽管它们区分大小写。
发布于 2016-10-06 08:40:05
正如克劳斯所说,答案很简单:不。但是,您可以选择一条更艰苦的路线,枚举顶级目录(os.path, glob)中的所有文件夹/文件,将其转换为小写(string.lower),测试相等,逐步下降一级,等等。
这对我来说很管用:
import os
def match_lowercase_path(path):
# get absolute path
path = os.path.abspath(path)
# try it first
if os.path.exists(path):
correct_path = path
# no luck
else:
# works on linux, but there must be a better way
components = path.split('/')
# initialise answer
correct_path = '/'
# step through
for c in components:
if os.path.isdir(correct_path + c):
correct_path += c +'/'
elif os.path.isfile(correct_path + c):
correct_path += c
else:
match = find_match(correct_path, c)
correct_path += match
return correct_path
def find_match(path, ext):
for child in os.listdir(path):
if child.lower() == ext:
if os.path.isdir(path + child):
return child + '/'
else:
return child
else:
raise ValueError('Could not find a match for {}.'.format(path + ext))https://stackoverflow.com/questions/39890750
复制相似问题