Finding particular path in directory in Python

11,103

Assuming the date 20170423 is a file named accordingly. Then you could use os.walk():

start = "C:\\Users\\abc\\"

for dirpath, dirnames, filenames in os.walk(start):
    for filename in filenames:
        if filename == "20170412":
            filename = os.path.join(dirpath, filename)
            print(filename)
            print(dirpath)

If only a single file exists C:\Users\abc\def\ghi\20170412 then the above would output:

C:\Users\abc\def\ghi\20170412
C:\Users\abc\def\ghi\

You can of course change the if statement to fit whether you want to check filename.startswith("2017") or anything else.

Warning: If you do start = "C:\\" then this is most likely gonna take a looong time to finish. Because it is going to run through every single directory on the C drive.

You can equally change for filename in filenames

for dirname in dirnames:
    if dirname == "20170412":
        dirname = os.path.join(dirpath, dirname )
        print(dirname)
        print(dirpath)

Thus if C:\Users\abc\def\ghi\20170412 is actually a directory, then the above would output the same as before:

C:\Users\abc\def\ghi\20170412
C:\Users\abc\def\ghi\

I was hoping for something more succinct

You could still pack it into a list comprehension if that's what you call more brief.

files = [os.path.join(dir, f) for dir, dirs, files in os.walk(start) for f in files if f == "20170412"]
Share:
11,103
uniXVanXcel
Author by

uniXVanXcel

Updated on June 14, 2022

Comments

  • uniXVanXcel
    uniXVanXcel almost 2 years

    How could I find the path of the directory which contains a date like 20170423 ? meaning, it could be any date, but i want to have that specific path until i get a folder that contains a date.. There may be several sub-directories along the way that contain that pattern, but that's a special case, where i would need to give more precision, like the content of the folder to select the proper one. give it a shot if you d like for the special case, but for the case where i know that only one folder along the way contains that pattern, the way i start is :

     directPaths_list  =  [f.path for f in os.scandir( start ) if f.is_dir()   ] 
    

    This gives me all the paths from the start folder, to the end. Now, i would like to return the paths that have the date pattern in them, and in particular one, that has let's say whose int(folder_name) is < datetime.datetime.today()

    I could come up with a rough solution, but SO has people with very good skills at coming up with succint elegant solutions, so there it is.. any idea? thanks!

    for example, for the path start= C:/ this would return C:\Users\abc\def\ghi\20170412

    I was thinking something like this could work:

    [f.path for f in os.scandir('C:\\Users\\abc\\def\\ghi\\') if f.is_dir() and str(f.path).endswith(str(2),-2,-1) ]