0

I have a list of directories with same structure/same file name. Under each there are files with different types and each directory has the same file names (number of files may vary) but different contents.

parentDIR-
     - subDir1
       - file1.ext1 (contains taskID)
       - file2.ext2 (contains userID)
       - file3.ext3
     - subDir2
       - file1.ext1 (contains taskID)
       - file2.ext2 (contains userID)
       - file3.ext3
     - subDir100

How do I search all the directories and extract taskID and userID as a pair from file1/file2? I need the file1/file2 in the same directory to be opened/closed properly and read the contents because they have some information shared in common.

I am thinking to use

glob, with xxxx as, ExitStack(), but not sure how to connect them all together


1
  • What have you tried yourself so far? Commented Jul 22, 2020 at 5:00

2 Answers 2

1

You can use regex in glob

In [1]: import glob
   ...: for x in glob.glob('parent/**/*.ext[1|2]', recursive=True):
   ...:     # Open, read content close here
   ...:     print(x)
   ...:
parent/subdir2/file1.ext1
parent/subdir2/file2.ext2
parent/subdir1/file1.ext1
parent/subdir1/file2.ext2
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, in my case, ext1, ext2 are actually quite different pattern, e.g., abc.xy1, cde.dat.zst etc
@tudou you can customize the regex as per your needs
0

I used a slightly different approach

types={"*.ext1", "*.ext2"}

for type in types:
    path(dir_name).rglob(type):
        do_something

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.