Python从硬盘读取文件并根据找到的模式返回结果

0 投票
1 回答
2484 浏览
提问于 2025-04-17 03:08

我想做的是这样的事情;

从硬盘读取文件,找出像the这样的模式,看看文件里是否包含这个字符串。如果包含,就返回真(true),如果不包含,就返回假(false)。在函数调用时,应该友好地打印出类似`在file.txt中找到了the`的信息。

这是我目前想到的内容:

import os
path = '../'
folder = os.listdir(path);

y = {}
n = {}

def bla(pattern):
    for book in folder:
        if book[-3:] == 'txt':
            data = open(path+''+book).read()
            if pattern in sanitize(data):
                y[pattern] = book + " contains " + pattern
                return True
            else :
                n[pattern] = book + " does not contain " + pattern
                return False

if bla('jane'):
    print(y['jane']) 
    print(n['jane'])

我想要的输出结果是这样的:

1.txt 包含 'the'
2.txt 不包含 'the'
3.txt 包含 'the'
4.txt 不包含 'the'

这个方法可以工作,但没有我想要的返回真和返回假的功能,有没有更好的方法呢?

import os
path = '../'
folder = os.listdir(path);

def bla(pattern):
    for book in folder:
        if book[-3:] == 'txt':
            data = open(path+''+book).read()
            if pattern in sanitize(data):
                print(book + " contains " + pattern)
            else :
                print(book + " does not contain " + pattern)

bla('the')

1 个回答

1
import re
m1 = re.compile(r'.*?\.txt$')
pattern = 'yourpattern'
m2 = re.compile(r'%s' % (pattern))

for file in filter(m1.search, os.listdir(somedir)):
    if m2.search(open(file,'r').read()):
        print file, 'contains', pattern
    else:
        print file, 'does not contain', pattern

根据你的喜好来修改输出内容

撰写回答