how to check a file if you don't know his path?

426 Views Asked by At

I have a file and start it like a command (without full path):

os.system('start rvpush')

or

os.system('start rvpush.exe')

Both start notepad. But if I delete the file it will stop working and generate an error. Everything is logical...

That's a problem. Before I start I need to check if file exists or not. But I don't know where the file is stored.

os.path.exists('rvpush.exe')

return: False... but file exists and started...

I try to get folders from os.environ['PATH'] but the file is not found in these folders...

I'm confused. The file started and worked, but in PATH environment there is no path to file... How to check a file if you don't know his path?

PS: The task is to check whether the file exists and then run it. I'm not sure that all computers have this file and should be checked before starting. So I'm not sure the file is in the same place, so use just as a command rather than through the full path. In addition to the computer can be windows or linux...

PSS: The disc has many folders, so that the option to just check the whole disk in search of the file is not suitable (it could take a very long time).

2

There are 2 best solutions below

1
On
try:
    os.system('start rvpush.exe')
except IOError as e:
    # log the error somehow, e.g. logging.warning(e)
    # do whatever you need to do here if the file doesn't exist
2
On

back to my initial comment.. the way any given terminal would find a given binary to run is to start with the PATH variable, and recursively look at every file in each folder in the path.. (in *nix this usually includes \bin and \usr\bin etc..) this is no different than pre-defining some likely places to look and exhaustively checking all the files to see if it's there. many programs will add themselves to the path at install time so that the system will be able to find them, but if you just have a random executable binary sitting in any old folder. there is no way to find it other than exhaustively check.

That said, exhaustively checking may not be so bad as you think.. with some reasonable checks and balances in place you can likely enumerate all the likely places in relatively little time. Python is rather slow, and I was able to enumerate almost 30K filenames in both my program files and program files (x86) folders in about 6 seconds..

import os
from time import time

t = time()

executables = 0
total = 0
for root, dirs, files in os.walk('C:/Program Files/'):
    total += len(files)
    executables += sum(file.endswith('.exe') for file in files)

for root, dirs, files in os.walk('C:/Program Files (x86)/'):
    total += len(files)
    executables += sum(file.endswith('.exe') for file in files)

print 'found {} executables of {} files in {} seconds'.format(executables, total, time()-t)

prints:

>>>runfile('C:/Users/**/untitled0.py', wdir='C:/Users/**/python/test')
found 633 executables of 29301 files in 6.16199994087 seconds

I only tested this one method, and it's conceivable you could find a faster way to grab all the filenames all at once and then possibly use regex to find what you're looking for. Additionally I would save the location in some sort of config file for future lookups.. do the initial search during install, and provide some sort of loading / installing animation to placate users.