0

我想了解如何搜索文件名,而不考虑大小写或变化。似乎 listdir 方法只接受确切的名称。

例如:

文件路径:C:\user\files\Hello 文件

这行得通

  • 搜索字符串 'Hello' 将返回 C:\user\files\Hello 文件

这不

  • 搜索字符串“HELLO”或任何其他变体(大写)将不返回任何内容

有解决办法吗?

4

1 回答 1

1

你可以使用re像,

>>> import os
>>> os.listdir('.') # listing the files for demonstration
['hello', 'foo.zip', 'Hello']
>>> import re, glob
# search for file ending with `hello`,
>>> [f for f in os.listdir('.') if re.search('hello$', f, flags=re.IGNORECASE)]
['hello', 'Hello']
于 2019-05-10T14:34:52.273 回答