Python Forum
find a string in multiple files and display the output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find a string in multiple files and display the output
#1
I have tried to search a string from a file and it worked.

>>> if 'finish' in open('/home/user/python/drsync-test-aix.log').read():
...     print "true"
...
true

But I am unable to find how to search the same string in multiple files that end with .log extension, can someone help me
Reply
#2
You need to
  • create an empty list
  • Create a loop
  • open each file
  • test for 'finish'
  • if you find it, append filename to list
Reply
#3
Hello!

You can run through a directory:

For both:
import glob
import os.path
import os
# for a single directory
for file in glob.glob('/path/*.log'):
    files_list.append(os.path.abspath(file)
or the directory tree
for root, dirs, files in os.walk("path"):
    if files:
        for file in files:
            if os.path.splitext(file) == '.log':
                files_list.append(os.path.abspath(file))

and search for such a files. For every file get the absolute path and store all these paths for files in a list. Then can open files from the list with a for loop.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Thanks wavic it worked, how mail the below output
>>> for file in glob.glob('/path/*.log'):
...     if 'Finish' in open(file).read():
...             s2=file.split('-')[1]
...             print s2, " DR sync is successful"
...     else:
...             s2=file.split('-')[1]
...             print s2, " DR sync is failed"
...
Output:
A2D  DR sync is successful B2D  DR sync is successful C2D  DR sync is failed
Edit admin use code tag
Reply
#5
Don't know. Never done it myself.
See smtplib

It may work but see os.path and glob modules and what can you do with them. Learning is essential
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 527 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,533 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Find a specific keyword after another keyword and change the output sgtmcc 5 806 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  splitting file into multiple files by searching for string AlphaInc 2 878 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  How to display <IPython.core.display.HTML object>? pythopen 3 45,908 May-06-2023, 08:14 AM
Last Post: pramod08728
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,146 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 955 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Find duplicate files in multiple directories Pavel_47 9 3,067 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  merge two csv files into output.csv using Subprocess mg24 9 1,756 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  Python: re.findall to find multiple instances don't work but search worked Secret 1 1,205 Aug-30-2022, 08:40 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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