×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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.


CODE --> 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) 

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

Quote (Tester_V)


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 

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

Quote (Tester_V)


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: https://www.tek-tips.com/viewthread.cfm?qid=180217...

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).

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

(OP)
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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close