Python Forum
Find specific subdir, open files and find specific lines that are missing from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find specific subdir, open files and find specific lines that are missing from a file
#1
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 Smile but not the one that are missing. Sad
Thank you.

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


Messages In This Thread
Find specific subdir, open files and find specific lines that are missing from a file - by tester_V - Aug-22-2020, 09:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to find difference between fake driving license or real using python? pjaymn 5 2,792 Jun-14-2024, 07:01 AM
Last Post: Pedroski55
  How to compare specific elements of a TSV in difflib gonksoup 1 184 Jun-08-2024, 02:05 PM
Last Post: Gribouillis
  Schedule exit a program at a specific time 4 am every day. chubbychub 3 382 May-17-2024, 03:45 PM
Last Post: chubbychub
  Regex to find triple characters bfallert 14 722 May-16-2024, 04:02 PM
Last Post: xMaxrayx
  Writing a cycle to find the nearest point from the array Tysrusko 0 248 May-10-2024, 11:49 AM
Last Post: Tysrusko
Question Right way to open files with different encodings? Winfried 2 423 Apr-23-2024, 05:50 PM
Last Post: snippsat
  Open files in an existing window instead of new Kostov 2 495 Apr-13-2024, 07:22 AM
Last Post: Kostov
  delete specific row of entries jacksfrustration 3 534 Feb-13-2024, 11:13 PM
Last Post: deanhystad
  Extracting specific file from an archive tester_V 4 680 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Open/save file on Android frohr 0 437 Jan-24-2024, 06:28 PM
Last Post: frohr

Forum Jump:

User Panel Messages

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