Finding particular path in directory in Python

梦想与她 提交于 2020-12-29 08:09:37

问题


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) ] 

回答1:


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"]


来源:https://stackoverflow.com/questions/43553742/finding-particular-path-in-directory-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!