首页 > 解决方案 > 用于查找包含特定多个字符串的文件的 Python 脚本(不保证按顺序排列)

问题描述

我想在给定文件夹中查找包含特定给定字符串的文件。例如:

我有一个“学生”文件夹,其中包含包含学生信息的文件(名字、姓氏和他们的家乡、他们的 GPA 等,这些文件不按顺序排列) - 例如:

student1.txt | student2.txt | student3.txt | student4.txt | student5.txt | student6.txt
John         | Kim          | Barnes       | Steven       | 2.0          | Terry
Barnes       | Pen          | John         | Horns        | Barnes       | McGinnis
Detroit      | Gotham       | Detroit      | Chicago      | Detroit      | Gotham
4.0          | 2.4          | 3.5          | 2.6          | John         | 2.8

如您所见,在我们的理论情况下,学生 1、3 和 5 都有相同的名字、姓氏和城市,但他们的 GPA 不同。

所以我想问一下如何输入“John”、“Barnes”、“Detroit”,并将输出作为包含相同 3 个必需字符串的文件的名称:

student1
student3
student5

到目前为止,我有(用于查找文件夹路径的 OS 模块):

import os

# The path is taken care of - I don't want to waste space here
for filename in os.listdir(path):

   fname = input("Enter first name: ")
   lname = input("Enter last name: ")
   city = input("Enter home city: ")

   line = file.readline()
   line_num = 1

   while line != '':
      #search for strings' indexes
      fn_index = line.find(fname)
      ln_index = line.find(lname)
      city_index = line.find(lname)
   
      if(index != -1)
         # I don't know what to do here 

         # My desired output (filename was taken care of)
         print(filename, fname, lname, homecity)

      line_num += 1

   file.close()

请注意,fname、lname、city 和 GPA不按顺序排列。以上只是一个例子。

就这样。如果我的问题已经回答,请告诉我。谢谢!

标签: python

解决方案


忽略顺序,以列表形式查看

import os

path = r"Your PATH"

fname = input("Enter first name: ")
lname = input("Enter last name: ")
city = input("Enter home city: ")

for filename in os.listdir(path):
    with open(os.path.join(path, filename)) as file:
        lines = [line.rstrip('\n') for line in file.readlines()]
        if ((fname in lines) and (lname in lines) and (city in lines)):
            # Desired output (filename was taken care of)
            print(filename, fname, lname, city)
        file.close()

推荐阅读