-1

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 \dir1\subdir1\file1.txt and \dir2\subdir1\file1.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

Alexander
  • 4,527
  • 5
  • 51
  • 98

2 Answers2

1

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())
Сергей Кох
  • 1,417
  • 12
  • 6
  • 13
1

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))
Timus
  • 10,974
  • 5
  • 14
  • 28
  • I didn't get your solution why you are adding `file_1` path and [1:]. The problem is if the same file exist in the same subdirs of file_1 and file_2 path. in this case file1.txt exist in both of them so open them and start to do your logic. – Alexander Sep 23 '22 at 15:59
  • @Alexander I don’t quite understand the question: `file_1.parts[1:]` is the parts of `file_1` path-parts (the parts that are separated by \ or /), except the first part `dir1`. Just out of curiosity: Did it work (worked here)? (If it didn’t, I suspect `dir1` contains path separators.) – Timus Sep 23 '22 at 16:44