How To Call A Specific Text File By Giving Only Its Name(not By Giving Full Path For Reading The File) From Sub-directories In Python?

- 1 answer

I want to read the specific text file by giving only its name(not full path) from the folder containing multiple sub-directories, in order to compare and merge the text files.

How to can I read the specific file using pandas ?



You can use walk to search for the file and then work with it

import os
name = "Name of your file"
root= './' #Assuming you will search from current directory, you can give any path = []

for dirpath, subdirs, files in os.walk(root):
    for x in files:
        if x==name:
            path.append(os.path.join(dirpath, x))
print(path) #do your operation here