简体   繁体   English

不区分大小写的文件名模式匹配返回 None

[英]Case-insensitive filename pattern matching returns None

Update: How do I return a string from a regex match in python?更新: 如何从 python 中的正则表达式匹配返回字符串? - this question seems similar and I've updated accordingly. - 这个问题似乎很相似,我已经相应地更新了。


I'm trying to open a file to write to it but, I don't know its case, so I'm trying to case-insensitive match its filename from its path.我试图打开一个文件来写入它,但是,我不知道它的大小写,所以我试图不区分大小写地从它的路径匹配它的文件名。

Currently, with print(my_file) , I see all the files in the folder_path but, file_match returns None.目前,使用print(my_file) ,我可以看到folder_path中的所有文件,但是file_match返回 None。 I also tried re.search instead of re.match and it returns None as well.我也尝试过 re.search 而不是 re.match 并且它也返回 None 。

import re
from pathlib import Path as p
cwd = p.cwd()

folder_path = p(cwd / 'somefolder')

for my_file in folder_path.iterdir():
    print(my_file)
    file_match = re.search('.+/test.txt', str(my_file), re.IGNORECASE)
    print(file_match) # Gives me <re.Match object; span=(0, 97), match='/the/path>

print(file_match) # gives me None

I'm hoping to match one of the following etc:我希望匹配以下之一等:

  • /cwd/somefolder/test.txt /cwd/somefolder/test.txt
  • /cwd/somefolder/TEst.Txt /cwd/somefolder/TEst.Txt
  • /cwd/SOMEFolder/teSt.TXT /cwd/SOMEFolder/teSt.TXT

but not this:但不是这个:

  • /cwd/somefolder/NotTest.txt /cwd/somefolder/NotTest.txt
  • /cwd/somefolder/Other.mp3 /cwd/somefolder/Other.mp3

Ultimately, I want a variable that includes the path to the /cwd/somefolder/test.txt, just in any case it happens to exist in. This way I can open and write to said file.最终,我想要一个包含 /cwd/somefolder/test.txt 路径的变量,无论如何它碰巧存在。这样我就可以打开并写入所述文件。

Here's the regular expression I'm using, which appears to be working: https://regex101.com/r/hz5qNe/2这是我正在使用的正则表达式,它似乎有效: https : //regex101.com/r/hz5qNe/2

Note: there is only one test.txt in each folder I'm searching in but, I don't know its case ie the filename is always the same but, the case can be different depending on the current folder_path iteration (I'm looping to find each folder's test.txt path but, didn't include that in the above code, as to par it down to its simplest form.)注意:我正在搜索的每个文件夹中只有一个 test.txt,但是,我不知道它的大小写,即文件名始终相同,但是,大小写可能会有所不同,具体取决于当前的 folder_path 迭代(我是循环查找每个文件夹的 test.txt 路径,但没有将其包含在上面的代码中,以便将其分解为最简单的形式。)

Any help would be appreciated, thanks!任何帮助将不胜感激,谢谢!

I would suggest dropping re and use the following test:我建议放弃re并使用以下测试:

if my_file.name.lower() == 'test.txt':
    ...

This should make the code also easier to understand and maintain.这应该使代码也更容易理解和维护。

If you then need to access the folder name you can just use the my_file.parent property.如果您需要访问文件夹名称,您可以使用my_file.parent属性。

You need to pull it out of the regex Match Object using .group(), like this demo - ( source )您需要使用 .group() 将它从正则表达式匹配对象中拉出来,就像这个演示- (

import re
from pathlib import Path as p
cwd = p.cwd()

folder_path = p(cwd / 'somefolder')

for my_file in folder_path.iterdir():
     file_match = re.search('.+/test.txt', str(my_file), re.IGNORECASE)
     if file_match:
         match = file_match.group()

print(match) # prints: /cwd/somefolder/test.txt (or any case of test.txt, which ever case is present...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM