简体   繁体   English

如何找到特定文件的路径

[英]how to find the path of a specific file

I want to find the full path to a file/folder by it's name only.我只想通过名称找到文件/文件夹的完整路径。 Is it possible?是否可以?

Assuming the file is in your working dir :假设文件in您的工作dir

Using os :使用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 :编辑 2

If you only want to check in a specific dir :如果您只想签入特定dir

for r,d,f in os.walk('d:\\'):
    for files in f:
        if files == "db.jpg":
            print(os.path.join(r,files))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM