How can I find path to given file?

后端 未结 6 1333
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 22:00

I have a file, for example \"something.exe\" and I want to find path to this file
How can I do this in python?

6条回答
  •  粉色の甜心
    2020-12-16 22:26

    if you absolutely do not know where it is, the only way is to find it starting from root c:\

    import os
    for r,d,f in os.walk("c:\\"):
        for files in f:
             if files == "something.exe":
                  print os.path.join(r,files)
    

    else, if you know that there are only few places you store you exe, like your system32, then start finding it from there. you can also make use of os.environ["PATH"] if you always put your .exe in one of those directories in your PATH variable.

    for p in  os.environ["PATH"].split(";"):
        for r,d,f in os.walk(p):
            for files in f:
                 if files == "something.exe":
                     print os.path.join(r,files)
    

提交回复
热议问题