我是靠谱客的博主 彪壮小懒虫,最近开发中收集的这篇文章主要介绍python读取配置文件不更改大小写_Python不区分大小写的文件名?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

如果不列出一个目录,并将要查找的项和目录中的每个项都转换为一个常见的案例进行比较,则无法执行此操作。文件系统是区分大小写的,这就是它的全部功能。def path_insensitive(path):

"""

Get a case-insensitive path for use on a case sensitive system.

>>> path_insensitive('/Home')

'/home'

>>> path_insensitive('/Home/chris')

'/home/chris'

>>> path_insensitive('/HoME/CHris/')

'/home/chris/'

>>> path_insensitive('/home/CHRIS')

'/home/chris'

>>> path_insensitive('/Home/CHRIS/.gtk-bookmarks')

'/home/chris/.gtk-bookmarks'

>>> path_insensitive('/home/chris/.GTK-bookmarks')

'/home/chris/.gtk-bookmarks'

>>> path_insensitive('/HOME/Chris/.GTK-bookmarks')

'/home/chris/.gtk-bookmarks'

>>> path_insensitive("/HOME/Chris/I HOPE this doesn't exist")

"/HOME/Chris/I HOPE this doesn't exist"

"""

return _path_insensitive(path) or path

def _path_insensitive(path):

"""

Recursive part of path_insensitive to do the work.

"""

if path == '' or os.path.exists(path):

return path

base = os.path.basename(path) # may be a directory or a file

dirname = os.path.dirname(path)

suffix = ''

if not base: # dir ends with a slash?

if len(dirname) < len(path):

suffix = path[:len(path) - len(dirname)]

base = os.path.basename(dirname)

dirname = os.path.dirname(dirname)

if not os.path.exists(dirname):

dirname = _path_insensitive(dirname)

if not dirname:

return

# at this point, the directory exists but not the file

try: # we are expecting dirname to be a directory, but it could be a file

files = os.listdir(dirname)

except OSError:

return

baselow = base.lower()

try:

basefinal = next(fl for fl in files if fl.lower() == baselow)

except StopIteration:

return

if basefinal:

return os.path.join(dirname, basefinal) + suffix

else:

return

最后

以上就是彪壮小懒虫为你收集整理的python读取配置文件不更改大小写_Python不区分大小写的文件名?的全部内容,希望文章能够帮你解决python读取配置文件不更改大小写_Python不区分大小写的文件名?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部