I want to find the full path to a file/folder by it's name only. Is it possible?
Asked
Active
Viewed 637 times
1 Answers
1
Assuming the file is in
your working dir
:
Using os
:
import os
print(os.path.abspath("image.png"))
OUTPUT:
C:\Users\dirtybit\PycharmProjects\opencv-basics\image.png
EDIT:
Let's say you have no idea where it is:
import os
dirs = ['c:\\','d:\\'] # your desired dirs to search under
for dir in dirs:
for r,d,f in os.walk(dir):
for files in f:
if files == "db.jpg":
print(os.path.join(r,files))
OUTPUT:
C:\Users\dirtybit\PycharmProjects\opencv-basics\image.png
EDIT 2:
If you only want to check in a specific dir
:
for r,d,f in os.walk('d:\\'):
for files in f:
if files == "db.jpg":
print(os.path.join(r,files))

DirtyBit
- 16,613
- 4
- 34
- 55
-
by using that code I'm only getting the path to the place I'm running the code from Is there any other way – שחר אימלפרב Mar 26 '19 at 10:43
-
1thank you very much it helped me a lot, I have one last question can I found with that specific dir? – שחר אימלפרב Mar 26 '19 at 10:47
-
@שחראימלפרב Kindly check the edit 3. PS. If this answer helped you may accept it: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work cheers! – DirtyBit Mar 26 '19 at 10:50
-
sorry I wasn't clear, I want to search for the specific dir – שחר אימלפרב Mar 26 '19 at 10:50
-
@שחראימלפרב sure you should look for the `d` in that case, I recommend reading: https://docs.python.org/2/library/os.html#os.walk – DirtyBit Mar 26 '19 at 10:54