How to open files when subdirs name and file names match

Question:

I have file structure like this

dir1
    subdir1
        -file1.txt
        -file2.txt

    subdir3
        -file2.txt
dir2
    subdir1
        -file1.txt
    subdir2
        -file2.txt

I want to use dir1 as reference directory and open file in dir2 when dir2 subdirs names match to that one in dir1. So basically open file1.txt in both dir1subdir1file1.txt and dir2subdir1file1.txt and also match the file names as well.

I can walk through the subdirs but cannot find the logic to compare them

for path, subdirs, files in os.walk(path_to_json) :
    for file in subdirs :
        print (file)

How can we do this ?

how to open files that match a pattern in a subdirectory

Asked By: Alexander

||

Answers:

You can create a path simply by replacing dir1 with dir2 and if there is such a file, then open both files.

import os

path = r"C:....dir1"
files_dir1 = []

# We make a list of all files in the directory dir1.

for root, dirs, files in os.walk(path):
    for file in files:
        files_dir1.append(os.path.join(root, file))

for name in files_dir1:
    name_dir2 = name.replace('dir1', 'dir2', 1)

    # Open files when a file with a new path exists.

    if os.path.isfile(name_dir2):
        with open(name, 'r') as f:
            print(name, f.read())

        with open(name_dir2, 'r') as f:
            print(name_dir2, f.read())
Answered By: Сергей Кох

You could try something like this:

from pathlib import Path

for file_1 in Path('dir1').rglob('*.*'):
    file_2 = Path('dir2', *file_1.parts[1:])
    if file_2.exists():
        print(str(file_1))
        print(str(file_2))

If you only want to go for txt-files then change .rglob('*.*') to .rglob('*.txt'). When there are files without an extension you could do:

for file_1 in Path('dir1').rglob('*'):
    if file_1.is_dir():
        continue
    file_2 = Path('dir2', *file_1.parts[1:])
    if file_2.exists():
        print(str(file_1))
        print(str(file_2))

If you only want the files from the first sublevel (exactly one subdir-depth) then you could try:

for file_1 in Path('dir1').glob('*/*.*'):
    file_2 = Path('dir2', *file_1.parts[1:])
    if file_2.exists():
        print(str(file_1))
        print(str(file_2))
Answered By: Timus
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.