简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM