简体   繁体   English

Python:提供一个字符串,打开文件夹名称中包含字符串的文件夹,然后打开/读取该文件夹中的文件

[英]Python: Provide a string, open folder that contains the string in the folder name, then open/read a file in that folder

I have a highly branched folder structure as shown below.我有一个高度分支的文件夹结构,如下所示。 I want to match a barcode, which is nested between other descriptors and open/read a file of interest in that barcode's folder.我想匹配一个条形码,它嵌套在其他描述符之间,并打开/读取该条形码文件夹中感兴趣的文件。 The contents of each XXbarcodeXX folder are basically the same.每个XXbarcodeXX文件夹的内容基本相同。
目录的前 3 个级别

I have tried to use os.walk() , glob.glob() , and os.listdir() in combination with fnmatch , but none yielded the correct folder.我尝试将os.walk()glob.glob()os.listdir()fnmatch结合使用,但都没有产生正确的文件夹。 and glob.glob() just returned an empty list which I think means it didnt find anything.glob.glob()刚刚返回一个空列表,我认为这意味着它没有找到任何东西。

The closest of which I did not let finish bc it seemed to be going top down through each folder rather than just checking the folder names in the second level.其中最接近的我没有让完成 bc 它似乎是自上而下遍历每个文件夹,而不仅仅是检查第二级中的文件夹名称。 This was taking far too long bc some folders in the third and fourth levels have hundreds of files/folders.这花费了太长时间,因为第三和第四级中的某些文件夹有数百个文件/文件夹。

import re

path='C:\\my\\path'
barcode='barcode2'
 
for dirname, dirs, files in os.walk(path):
    for folds in dirs:
        if re.match('*'+barcode+'*', folds):
            f = open(os.path.join(dirname+folds)+'FileOfInterest.txt', 'w')

The * in re.match regex you are using will probably generate an error ( nothing to repeat at position 0 ) since is using a quantifier (zero or more times) without any preceding token.您正在使用的re.match正则表达式中的*可能会产生一个错误( nothing to repeat at position 0 ),因为它使用了一个没有任何前面标记的量词(零次或多次)。 You may try to replace your regex with '..' + barcode + '..' .您可以尝试用'..' + barcode + '..'替换您的正则表达式。 This regex will match your expected barcode string between any two characters (except for line terminators).此正则表达式将匹配任何两个字符之间的预期barcode字符串(行终止符除外)。 In the command os.path.join you may join all the path's names and the desired file in the same command to avoid any issues with the specific OS separator.在命令os.path.join您可以在同一命令中加入所有路径的名称和所需的文件,以避免特定操作系统分隔符出现任何问题。

import os
import re

path='dirStructure'
barcode='barcode2'

for dirname, dirs, files in os.walk(path):
    for folds in dirs:
        if re.match('..' + barcode + '..', folds):
            f = open(os.path.join(dirname, folds, 'FileOfInterest.txt'), 'r')
            print(f.readlines())
            f.close

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

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