Find all files in a directory and process only ones that have a specific line, skip if does not
Find all files in a directory and process only ones that have a specific line, skip if does not
(OP)
Need help, I’m new to python,
I wrote a script that should find all files in a directory and process only ones that have a specific line and skip the ones that do not have the line. The specific line is ‘," Run Time’ it fails to process ONLY files I need, it processes all files.
All Lines to find:
‘," Run Time’
‘,” Start Time’
‘,” End Time’
‘Test_ID:’
‘Test Program Name:’
‘Product:’
Also,
Lines 1, 2 and 3 are repeating lines and I need them all,
Lines 4, 5 and 6 also repeating but I need to capture them only ones.
Here is the script I have:
I wrote a script that should find all files in a directory and process only ones that have a specific line and skip the ones that do not have the line. The specific line is ‘," Run Time’ it fails to process ONLY files I need, it processes all files.
All Lines to find:
‘," Run Time’
‘,” Start Time’
‘,” End Time’
‘Test_ID:’
‘Test Program Name:’
‘Product:’
Also,
Lines 1, 2 and 3 are repeating lines and I need them all,
Lines 4, 5 and 6 also repeating but I need to capture them only ones.
Here is the script I have:
CODE --> python
import os runtime_l = '," Run Time' start_tm = '," Start Time' end_tm = '," End Time' test_ID = ' Host Name: ' program_n = 'Test Program Name:' prod_n = 'Product:' given_path = 'C:\\02\\en15\\TST' for filename in os.listdir(given_path): filepath = os.path.join(given_path, filename) if os.path.isfile(filepath): print("File Name: ", filename) print("File Name\\Path:", filepath) with open(filepath) as mfile: for line in mfile: if runtime_l in line: # do something with the line print(line) if start_tm in line: # do something with the line print(line) if end_tm in line: # do something with the line print(line) if test_ID in line: # do something with the line print (line) if program_n in line: # do something with the line print (line) if prod_n in line: # do something with the line print (line) else: continue
RE: Find all files in a directory and process only ones that have a specific line, skip if does not
Hard to tell without a sample file what would be the best way, but generally there would be 2 cases :
Feherke.
feherke.github.io
RE: Find all files in a directory and process only ones that have a specific line, skip if does not
the File can be anything with the 6 lines inserted randomly.
The two questions I need help with are:
1. How I can test a file to make sure it has a line "Run Time", process it if it has the line, and skip the rest of the files in a directory?
2. how to print only first match and ignore the rest of the matched lines?
for example, I'm looking for a line in a file that has this string "Test Program Name:", the file can have 2 to 5 or even more lines with this string.
I need to find only the first matched line that has the string 'Test Program Name:' and if found I want the code to print the line and start looking for the next variable to match that is '‘Test_ID:'.
RE: Find all files in a directory and process only ones that have a specific line, skip if does not
I'm trying to answer your two questions:
1. You could select for processing only those files which contain "Run Time".
Look at the command
CODE
2. You can mark when you have found one of the strings.
See the usage of the dictionary strings_found in my example below
Here is the example:
I created some files in a directory tree
CODE
tester_v.py
CODE
Running and output:
CODE
RE: Find all files in a directory and process only ones that have a specific line, skip if does not
And I'm sure it works great but it is too complicated.
Is there any other way? a simpler way to do this?
I need to incorporate your snippet into my code and I cannot do that, it is too complicated.
I really appreciate your help, thank you again!
RE: Find all files in a directory and process only ones that have a specific line, skip if does not
Using the command with find and grep to select only those files which contain the strings you want to process, or using the dictionary to mark the strings found in file ?
The best way would be, if you take the program apart, step by step and print some variables to see how it works, then you will see that it is not complicated.
RE: Find all files in a directory and process only ones that have a specific line, skip if does not
That is why obvious things for you look very complicated to me and I'm trying to do everything in very simple way.
Here is how I would try to test a file if it has a "Run Time" line in it and it works, kind of.
In a directory 'C:\\02\\en15\\TST' I'm skinning I have 4 files:
Debug_1.log
Debug_2.log
Debug_3_NO_Run Time
Debug_3_NO_Run Time
First two Debug log files have the "Run Time" line and the last two do not have the lines. When I execute my script with "else" block disabled, the script prints correctly:
<_io.TextIOWrapper name='C:\\02\\en15\\TST\\Debug_1.log' mode='r' encoding='cp1252'>
<_io.TextIOWrapper name='C:\\02\\en15\\TST\\Debug_2.log' mode='r' encoding='cp1252'>
>>>
When I enable the "else" block the script produces an error for some reason.
else:
^
IndentationError: expected an indented block
>>>
CODE --> python
RE: Find all files in a directory and process only ones that have a specific line, skip if does not
The code you posted works with me with else too. As your error message said, you must have an IndentationError near of the else. Maybe you mixed tabs and spaces for indentation.
RE: Find all files in a directory and process only ones that have a specific line, skip if does not
RE: Find all files in a directory and process only ones that have a specific line, skip if does not