0

I'm doing a program in which Chimera needs to be opened, I'm opening it with:

def generate_files_bat(filename):
    f = open(filename, 'w')
    text = """echo off SET PATH=%PATH%;"C:\\Program Files (x86)\\Chimera 1.6.1\\bin" chimera colpeps.cmd"""
    print >>f, text
    f.close()

But I need to find Chimera apart from the computer the python program is running. Is there any way the path can be searched by the python program in any computer?

3
  • I am not familiar with chimera, but you could search in registry for it's path and then use it? If not then you will have to crawl through the possible locations (and maybe even whole drive). Commented Jun 5, 2013 at 11:50
  • Is it explicitly for Windows? In Linux you could use which to find the path of the executable. Maybe there is something similar for Windows. Commented Jun 5, 2013 at 11:53
  • You can use os.walk to find the executable: stackoverflow.com/a/1724723/599884 Commented Jun 5, 2013 at 11:55

3 Answers 3

1

Generally speaking, I don't think it is such a good idea to search the path for a program. Imagine, for example that two different versions were installed on the machine. Are-you sure to find the right one? Maybe a configuraition file parsed with the standard module ConfigParser would be a better option?

Anyway, to go back to your question, in order to find a file or directory, you could try to use os.walk which recursively walks trough a directory tree.

Here is an example invoking os.walk from a generator, allowing you to collect either the first or all matching file names. Please note that the generator result is only based on file name. If you require more advanced filtering (say, to only keep executable files), you will probably use something like os.stat() to extend the test.

import os

def fileInPath(name, root):
    for base, dirs, files in os.walk(root):
        if name in files:
            yield os.path.join(base, name)

print("Search for only one result:")
print(next(fileInPath("python", "/home/sylvain")))

print("Display all matching files:")
print([i for i in fileInPath("python", "/home/sylvain")])
Sign up to request clarification or add additional context in comments.

Comments

0

There is which for Linux and where for Windows. They both give you the path to the executable, provided it lies in a directory that is 'searched' by the console (so it has to be in %PATH% in case of Windows)

Comments

0

There is a package called Unipath, that does elegant, clean path calculations.

Have look here for the AbstractPath constructor

Example:

from unipath import Path
prom_dir = Path(__file__)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.