Python不区分大小写的文件名? [英] Python case insensitive file name?

查看:75
本文介绍了Python不区分大小写的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定文件名,我需要加载一个文件,但是我得到的名称不区分大小写."A.txt"实际上可以是"a.txt".如何快速执行此操作(不生成所有可能的名称并尝试每个名称)?

I need to load a file given it's name, but the name I get is case insensitive. "A.txt" could actually be "a.txt". How to do this the fast way (not generate all possible names and try each)?

推荐答案

您可以列出文件所在的目录( os.listdir ),并查看文件名是否匹配.可以通过对文件名使用小写并进行比较来完成匹配.

You could list the directory the file's in (os.listdir), and see if there are matches for your filename. The matching can be done by lower-casing both filenames and comparing.

这篇关于Python不区分大小写的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆