Python Forum
Test a file for a string without opening it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Test a file for a string without opening it?
#1
Hi,
I have a lot of files in a directory,
Is there any good way to test a file for a string without opening it?
String =’Run Time’
If string is in file :
	do something
I hear about a function ”any” but not sure how to use it, or maybe there is a better way?

thank you in advance!
Reply
#2
You need to read the file if you want to know what's in it.
Reply
#3
I was thinking to use a "break" if the first 'Run Time" line found.
Then reload the file again and process it to the end of the file.
something like this:
                with open(currentF) as mfiler:  
                    for ln in mfiler:
                        if runtime_l in ln :
                            ln=ln.rstrip()
                            print ("Run Time line FOUND__>", ln)
                            ftoiter = currentF                            
                            break 

                    with open(ftoiter) as mfile:  
                        for lns in mfile:
                            if runtime_l in lns :
                                print ("RUN TIME line++++++++++++++ >", lns)
                                print (lns)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to replace a string with a file (HTML file) tester_V 1 698 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 918 Feb-15-2023, 05:34 PM
Last Post: zsousa
  How to sort .csv file test log which item first fail and paint color SamLiu 24 4,683 Sep-03-2022, 07:32 AM
Last Post: Pedroski55
  Facing Problem while opening a file through command prompt vlearner 4 1,856 Jan-30-2022, 08:10 AM
Last Post: snippsat
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,063 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to make a test data file for the full length of definition? MDRI 6 3,479 Apr-16-2021, 01:47 AM
Last Post: MDRI
  Subprocesses not opening File Select Dialog teut 2 2,359 Feb-22-2021, 08:07 PM
Last Post: teut
  Writing unit test results into a text file ateestructural 3 4,653 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Opening file and outputting its text content example leodavinci1990 1 1,848 Oct-12-2020, 05:33 AM
Last Post: buran
  Print a certain string only the first time it appears in a test file buttercup 5 2,697 Jul-23-2020, 01:30 PM
Last Post: palladium

Forum Jump:

User Panel Messages

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