0

I want to construct a function using subprocess on files without knowing their path : I've tried all the os.path functions, but it seems to work only in the current directory :

import os.path
os.path.abspath('file')
>>>"current/path/of/the/script/file"

But the file is not in the current directory and this output path is false. Is there a way to retrieve the path of a given file ?

EDIT: this post for finding files is interesting if you know where to search. But the path tree of the system which contains the file is not known. The file names are normalized and uniques (no doubles files in all the system).

Community
  • 1
  • 1
Baalito
  • 67
  • 8
  • 3
    Possible duplicate of [Find a file in python](http://stackoverflow.com/questions/1724693/find-a-file-in-python) – Mike Scotty Apr 28 '17 at 09:24
  • 1
    Where is the file? If the file is *"given"* how is it given? – Peter Wood Apr 28 '17 at 09:28
  • How is the application being supplied the file? – Cov Apr 28 '17 at 09:29
  • Perhaps, thank you for the link; the confirmed answer indicates to enter a parent path; but if we don't know it ? I'm coding an API to subprocess on files with normalized filenames, API that will be used per several users, not only on classics unix systems, but HPC. – Baalito Apr 28 '17 at 09:34
  • If you can't supply some directory that contains the file in one of its subdirectories then you'll have to search _every_ drive that the system has access to. That could take some time... Also consider what you want the program to do if it finds multiple files that have the given file name. – PM 2Ring Apr 28 '17 at 09:37
  • If you can specify the file name in the form of a Unix glob pattern then the [`glob`](https://docs.python.org/3/library/glob.html) module can be used to locate the file. – PM 2Ring Apr 28 '17 at 09:42

1 Answers1

0

Try this. You need to give it your Drive name and file name.

import os
def find(file_name, drive):
    for root, dirs, files in os.walk (drive):
        if file_name in files:
            return root + "//" + file_name
    return "FAIL"
print (find ("test.py", "U:"))
Minion Jim
  • 1,239
  • 13
  • 30