Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find specific subdirs, open files and find specific lines that are missing from a file 1

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
0
0
US
Hi,

I have a directory with bunch of subdirectories each subdir has a one file only,
I need to process files only form the subdirectories that have letter “H” in a name.
Each file will contain lines with the words "CELL-1", "CELL-2" up to "CELL-12 "- I’m interested in those lines .
I'd like to scan the file line by line and find/print "CELL-XX" lines for processing that are present in a file and the ones that are missing from a file.

Something like this:

output_file_a.write()
line CELL-1 -missing
line CELL-2 - infile
line CELL-3 -missing
and so on.....

output_file_b.write()
line CELL-1 -infile
line CELL-2 - infile
line CELL-3 -missing
and so on.....
I can find all the files and print out “CELL-xx” lines that are in each file but not the ones that are missing (CELL-xx lines).
Thank you.


Python:
import os
import pathlib

#path = 'c:/path_tosubdirs/'
mytof = 'H' 

for file in os.listdir(path):
    hdir_f = os.path.join(path, file)
    
    if mytof in hdir_f :                  ### Directories with 'H" in name
        path2 = hdir_f
        for file1 in os.listdir(path2):
            hdir_f1 = os.path.join(path2, file1)
            print ("DIR\path\file ->>",hdir_f1)

            with open (hdir_f1) as cells_file : 
                for el in cells_file :
                    if 'CELL-' in el :
                        el=el.rstrip()
                        print("CELL-xx ", el)
 
Tester_V said:
I have a directory with bunch of subdirectories each subdir has a one file only,
I need to process files only form the subdirectories that have letter “H” in a name.

For example you have the directory my_dir with subdirs containning H like
Code:
$ ls my_dir/*H*
my_dir/AHX:
file.txt

my_dir/AXH:
file.txt

my_dir/HBX:
file.txt

then you can get all those files using glob module like this:
Code:
>>> import glob
>>> my_files = glob.glob("my_dir/*H*/*")
>>> my_files
['my_dir/AHX/file.txt', 'my_dir/HBX/file.txt', 'my_dir/AXH/file.txt']
>>> for my_file in my_files:
...   print(my_file)
... 
my_dir/AHX/file.txt
my_dir/HBX/file.txt
my_dir/AXH/file.txt

 
Tester_V said:
I'd like to scan the file line by line and find/print "CELL-XX" lines for processing that are present in a file and the ones that are missing from a file.

I would do that like in the example tester_v.py which I posted in this thread for you:
In the function process_file modify the dictionary so, that it contains all patterns you want, e.g.:
Code:
  strings_found = {
    "CELL-1":False,
    "CELL-2":False,
    ...
    "CELL-12":False
  }
then at the function end print the results:
Code:
for key in strings_found.keys():
  if strings_found[key]:
    print("% - in file" % key)
  else:
    print("% - missing" % key)
and finally in the main create the list file_names using glob module (see my previous post).
 
To mikrom,
Great post!
I'm still at the beginning of my "Python road" and I need to learn a lot. I did not even try to use dictionary or functions.....
I'm sure there are some other ways how to do this I just do not see any and I appreciate your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top