简体   繁体   中英

How to get the full path of a directory given only the name of the directory in python

I am working on a pydev project where for some reason I consider a web application. I have the path of one file of the web application project. say I have -

C:\webflow_implementaton\SpringWebFlowFlightBooking-master\src\main\java\it\iol\springwebflow\Booking.java

With this information can I fetch the path where the "webapp" folder is located in the web application? Is it possible in python?

Thanks in advance

Would os.walk work for your case? Very rough example written below, im sure there is a quicker way to do it.

It takes your path, splits it such that you can go back to a default folder level of your choice. Then uses os.walk to go through all folders until it finds webapp.

import os

path = 'C:\\webflow_implementaton\\SpringWebFlowFlightBooking-master\\
        src\\main\\java\\it\\iol\\springwebflow\\Booking.java'

path_split = path.split("\\")[::-1]

#default folder depth to use
folders_deep = 1

for i in path_split[0:folders_deep]:
    path = path.replace("\\"+i,"")
print (path)

list_dir = os.walk(path)
for root, dirs, files in os.walk(path, topdown=False):
    for name in dirs:
        if "webapp" in name: 
            print(os.path.join(root, name))
        else:
            print("Folder not found")

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