简体   繁体   English

在不同文件夹中的.txt文件中进行Python文本搜索,并打印文件和文件夹的名称

[英]Python Text Searching in .txt files in different folders with printing the name of file and folder

I'm making a script in Python for searching for the selected term (word/couple words, sentence) in a bunch of .txt files in a selected folder with printing out the names of the .txt files which contain the selected term. 我正在用Python编写脚本,以在选定文件夹中的一堆.txt文件中搜索选定的术语(单词/成对单词,句子),并打印出包含选定术语的.txt文件的名称。 Currently is working pretty fine using os module: 当前使用os模块可以正常工作:

import os

dirname = '/Users/User/Documents/test/reports'

search_terms = ['Pressure']
search_terms = [x.lower() for x in search_terms]

for f in os.listdir(dirname):
    with open(os.path.join(dirname,f), "r", encoding="latin-1") as infile:
        text =  infile.read()

    if all(term in text for term in search_terms):
        print (f)

But I wanna make the extension for the script: to be able to search not only in one folder (dirname), but in two for example (dirname1, dirname2) which consist of also .txt files. 但是我想对脚本进行扩展:不仅可以在一个文件夹(目录名)中搜索,而且还可以在两个目录(目录名,目录名2)中搜索,其中还包括.txt文件。 Also I would like to print not only the name of the searched report but the name of the directory (dirname) where it locates. 另外,我不仅要打印搜索到的报告的名称,还要打印它所位于的目录(目录名)的名称。 Is it possible to do that using os module or there will be some other approaches to do that? 是否可以使用os模块来做到这一点,或者还有其他方法可以做到这一点?

You can iterate over dirnames like this : 您可以像这样遍历目录名:

import os

dirnames = ['/Users/User/Documents/test/reports','/Users/User/Documents/test/reports2']

search_terms = ['Pressure']
search_terms = [x.lower() for x in search_terms]
for dir_name in dirnames:
    for f in os.listdir(dir_name):
        with open(os.path.join(dir_name, f), "r", encoding="latin-1") as infile:
            text = infile.read()

        if all(term in text for term in search_terms):
            print("{} in {} directory".format(f, dir_name))

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

相关问题 如何使用 python 中的 file.txt 中的文件名重命名文件夹中的文件? - how to rename files in a folder with the files name in file.txt in python? 合并来自不同文件夹的多个文件,并通过 Python 将文件夹名称重命名为新文件夹 - Combine multiple files from different folders and renamed as folder name to new folder by Python 在 Python 中搜索 TXT 文件 - Searching TXT files in Python Python 3.6将文本输出为.txt文件 - Python 3.6 printing out the text in a .txt file 考虑到文件名包含 python 中目标文件夹的名称,如何将文件从一个文件夹复制到另一个文件夹 - How to copy files from one folder to another taking into account that the file name contains the name of the destination folders in python 使用带有字符串的文本文件并在文件夹中的文件中搜索字符串 - Using a text file with strings and searching for the strings in files in folders 使用 python 在不同文件夹中连接具有相同名称的.txt 文件 - Concatenate .txt files with same names in different folders with python Python将txt文件保存在另一个文件夹中 - Python save txt file in a different folder python从文件夹中读取所有文件,并将文件名和其他信息写入txt文件 - python read all files from a folder and write the file name and other info into a txt file 打印.txt文件Python - Printing a .txt File Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM