1

I want to find the full path to a file/folder by it's name only. Is it possible?

DirtyBit
  • 16,613
  • 4
  • 34
  • 55

1 Answers1

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