Search code examples
pythondirectorypathsubdirectoryfile-search

Search all files with same name in a directory python


I Have a Question : I need to get paths of a file in a directory, I have a folder that contains other folders and other folders etc.... and each of them contains a file "tv.sas7bdat" I need to get every path to that file. Thank you !!!


Solution

  • If I get your problem right you can achieve your goal using Pythons's os.walk function, like so:

    import os
    for root, dirs, files in os.walk("<starting folder here>", topdown=False):
        for name in files:
            if name == "tv.sas7bdat":
                print(os.path.join(root, name))
    

    p.s: as for comments in your question, next time please provide as many details possible in your question and provide code of your attempt, see the asking guidelines