在文件夹中搜索包含特定字符串的子文件夹,排除滞后值

2024-09-30 01:19:39 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在python中搜索字符串中的值,并且仅在该字符串不包含搜索字符串中的任何前导/滞后值时返回。白间距是可接受的,即

我想在下面有子文件夹的文件夹中搜索“WO1”或“WO1 BLD2 L1 H3 Fitout”

子文件夹:

WO1
WO123

SP10152100 WO137

WO1 BLD2 L1 H3 Fitout

我使用的代码返回“SP10152100 WO137”

temp_WO_dir1 = "WO" + wo
path = root + "\\"+ dir
for root, dirs, files in os.walk(path):
    for dir in dirs:
        print dir
        if temp_WO_dir1 in dir:
            find = True 
            sp_path = root +  "\\" + dir + "\\3 Finance\\Telstra Invoicing"
            print sp_path
            break

    if root.count(os.sep) - path.count(os.sep) == 0:
    del dirs[:]

更新:

尝试了以下操作但不起作用

if re.match(temp_WO_dir1+"\s" , dir):

Tags: path字符串in文件夹l1ifosdir

热门问题