質問

When writing a Python code, I could write os.system('some cmd command'). How could I make it run from a specific directory (without using a 'cd' command)?

The reason I'm trying to do this is because my cmd command isn't in my cmd PATH, and therefore is recognized only in its own directory.

I'm looking for something similar to this:

p = Popen ('file.bat', cwd = 'location')
stdout, stderr = p.communicate()

just without having to create a .bat file for it...

役に立ちましたか?

解決

You could always do:

p = Popen ('../relative/path/to/executable.exe')
stdout, stderr = p.communicate()

Or you could hard-code an absolute path in there, but be sure to check if the file exists first or to catch any exceptions thrown by the popen calls (more pythonic).

LE: Please note that there is a cwd parameter on the Popen's constructor, but the executed program's current directory will be changed to that, it won't look for the executable in there.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top