简体   繁体   English

如果在文本文件中找到特定的字符串,则从文件夹内读取文本文件并保存文件夹的名称-Python

[英]Reading a text file from within a folder and saving the name of the folder if a particular string is found in the text file - Python

I have about 100 folders with random names, say for this example 1,2,3,4,...100. 我大约有100个带有随机名称的文件夹,例如1,2,3,4,... 100。 Inside these folders, I have text files with some strings in them. 在这些文件夹中,我有一些带有一些字符串的文本文件。 eg: sample.txt. 例如:sample.txt。

The text files all have the same name but are in different folders. 这些文本文件都具有相同的名称,但是位于不同的文件夹中。 What I need is to read the files from inside these folders, and read the text inside these files and print out or save the location of these text files. 我需要的是从这些文件夹中读取文件,并读取这些文件中的文本,然后打印或保存这些文本文件的位置。

I only know how to read lines from a file if it is in my pwd and look for stuff in it. 我只知道如何从文件中读取行(如果该文件位于我的pwd中)并在其中查找内容。 I use the following code for that: 我为此使用以下代码:

with open(r'Example.txt', 'r') as infile_txt:
    for line in infile_txt:
        if r"sample" in line:
            print line

How can I read files from inside folders and record the names of these folders? 如何从文件夹内部读取文件并记录这些文件夹的名称?

You can use os for that. 您可以为此使用os。 For example: 例如:

import os
list_downloads = os.listdir("C:/Users/user/Downloads")

This will give you all subfolders and files in a list. 这将为您提供列表中的所有子文件夹和文件。 You can then traverse the list to find needed subfolder and repeat the action. 然后,您可以遍历列表以找到所需的子文件夹并重复操作。

import os
import re
from os.path import join, getsize

with open('output.txt','w') as out_file:
    for root,subFolders, files in os.walk(r"C:\Users\USER007\Desktop\Python Scripts\Reading metadata\files"):
        if r'MetaData.txt' in files:
            with open(os.path.join(root, 'MetaData.txt'), 'r') as in_file:
                for lines in in_file:
                    if r'THIS' and r'THAT' and r'THOSE' in lines:
                        print root

The above code worked for me. 上面的代码为我工作。

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

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