Python Forum
Read Multiples Text Files get specific lines based criteria
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read Multiples Text Files get specific lines based criteria
#4
This works for me
# /usr/bin/env python3

import os

for root, dirs, files in os.walk('./', topdown=True):

    for name in files:
        if 'txt' in name:
            with open(name, 'r') as lines:
                for line in lines:
                    for word in line.split():
                        if '|D100|' in word:
                            print(line) 
Output:
this is some text with |D100| text with |D100| still more |D100| one more text with |D100|
Directory structure
Output:
├── another.txt ├── my.txt └── walk.py Contents of my.txt this is some text with |D100| this text does not have it more text text with |D100| Contents of another.txt This is another file text More here still more |D100| one more text with |D100|
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
RE: Read Multiples Text Files get specific lines based criteria - by menator01 - May-18-2020, 12:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 480 Feb-07-2024, 12:24 PM
Last Post: Viento
  filtering a list of dictionary as per given criteria jss 5 751 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Move Files based on partial Match mohamedsalih12 2 868 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Color a table cell based on specific text Creepy 11 2,110 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  Making a question answering chatbot based on the files I upload into python. Joejones 1 1,285 May-19-2023, 03:09 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,679 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  python print all files which contain specific word in it mg24 5 1,283 Jan-27-2023, 11:20 AM
Last Post: snippsat
  How to read in mulitple files efficiently garynewport 3 915 Jan-27-2023, 10:44 AM
Last Post: DeaD_EyE
  python move specific files from source to destination including duplicates mg24 3 1,137 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  azure TTS from text files to mp3s mutantGOD 2 1,741 Jan-17-2023, 03:20 AM
Last Post: mutantGOD

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020