Nika
Nika

Reputation:

How can I find path to given file?

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

Upvotes: 13

Views: 115891

Answers (6)

bduhbya
bduhbya

Reputation: 430

This is really old thread, but might be useful to someone who stumbles across this. In python 3, there is a module called "glob" which takes "egrep" style search strings and returns system appropriate pathing (i.e. Unix\Linux and Windows).

https://docs.python.org/3/library/glob.html

Example usage would be:

results = glob.glob('./**/FILE_NAME')

Then you get a list of matches in the result variable.

Upvotes: 2

Ivan Calderon
Ivan Calderon

Reputation: 620

Just to mention, another option to achieve this task could be the subprocess module, to help us execute a command in terminal, like this:

import subprocess

command = "find"
directory = "/Possible/path/"
flag = "-iname"
file = "something.foo"
args = [command, directory, flag, file]
process = subprocess.run(args, stdout=subprocess.PIPE)
path = process.stdout.decode().strip("\n")
print(path)

With this we emulate passing the following command to the Terminal: find /Posible/path -iname "something.foo". After that, given that the attribute stdout is binary string, we need to decode it, and remove the trailing "\n" character.

I tested it with the %timeit magic in spyder, and the performance is 0.3 seconds slower than the os.walk() option.

I noted that you are in Windows, so you may search for a command that behaves similar to find in Unix.

Finally, if you have several files with the same name in different directories, the resulting string will contain all of them. In consequence, you need to deal with that appropriately, maybe using regular expressions.

Upvotes: 1

Lennart Regebro
Lennart Regebro

Reputation: 172209

Uh... This question is a bit unclear.

What do you mean "have"? Do you have the name of the file? Have you opened it? Is it a file object? Is it a file descriptor? What???

If it's a name, what do you mean with "find"? Do you want to search for the file in a bunch of directories? Or do you know which directory it's in?

If it is a file object, then you must have opened it, reasonably, and then you know the path already, although you can get the filename from fileob.name too.

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342303

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)

Upvotes: 9

sunqiang
sunqiang

Reputation: 6492

use os.path.abspath to get a normalized absolutized version of the pathname
use os.walk to get it's location

import os
exe = 'something.exe'
#if the exe just in current dir
print os.path.abspath(exe)
# output
# D:\python\note\something.exe

#if we need find it first
for root, dirs, files in os.walk(r'D:\python'):
    for name in files:
        if name == exe:
            print os.path.abspath(os.path.join(root, name))

# output
# D:\python\note\something.exe

Upvotes: 20

Greg Hewgill
Greg Hewgill

Reputation: 992787

Perhaps os.path.abspath() would do it:

import os
print os.path.abspath("something.exe")

If your something.exe is not in the current directory, you can pass any relative path and abspath() will resolve it.

Upvotes: 23

Related Questions