Python Forum

Full Version: Test a file for a string without opening it?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
You need to read the file if you want to know what's in it.
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)