Test if executable exists in Python?

2020-01-23 05:39发布

In Python, is there a portable and simple way to test if an executable program exists?

By simple I mean something like the which command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with Popen & al and see if it fails (that's what I'm doing now, but imagine it's launchmissiles)

标签: python path
21条回答
该账号已被封号
2楼-- · 2020-01-23 06:01

So basically you want to find a file in mounted filesystem (not necessarily in PATH directories only) and check if it is executable. This translates to following plan:

  • enumerate all files in locally mounted filesystems
  • match results with name pattern
  • for each file found check if it is executable

I'd say, doing this in a portable way will require lots of computing power and time. Is it really what you need?

查看更多
Bombasti
3楼-- · 2020-01-23 06:02

I found something in StackOverflow that solved the problem for me. This works provided the executable has an option (like --help or --version) that outputs something and returns an exit status of zero. See Suppress output in Python calls to executables - the "result" at the end of the code snippet in this answer will be zero if the executable is in path, else it is most likely to be 1.

查看更多
劳资没心,怎么记你
4楼-- · 2020-01-23 06:02

An important question is "Why do you need to test if executable exist?" Maybe you don't? ;-)

Recently I needed this functionality to launch viewer for PNG file. I wanted to iterate over some predefined viewers and run the first that exists. Fortunately, I came across os.startfile. It's much better! Simple, portable and uses the default viewer on the system:

>>> os.startfile('yourfile.png')

Update: I was wrong about os.startfile being portable... It's Windows only. On Mac you have to run open command. And xdg_open on Unix. There's a Python issue on adding Mac and Unix support for os.startfile.

查看更多
淡お忘
5楼-- · 2020-01-23 06:06

It would seem the obvious choice is "which", parsing the results via popen, but you could simulate it otherwise using the os class. In pseudopython, it would look like this:

for each element r in path:
    for each file f in directory p:
        if f is executable:
           return True
查看更多
The star\"
6楼-- · 2020-01-23 06:07

Python 3.3 now offers shutil.which().

查看更多
Rolldiameter
7楼-- · 2020-01-23 06:07

For *nix platforms (Linux and OS X)

This seems to be working for me:

Edited to work on Linux, thanks to Mestreion

def cmd_exists(cmd):
    return subprocess.call("type " + cmd, shell=True, 
        stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0

What we're doing here is using the builtin command type and checking the exit code. If there's no such command, type will exit with 1 (or a non-zero status code anyway).

The bit about stdout and stderr is just to silence the output of the type command, since we're only interested in the exit status code.

Example usage:

>>> cmd_exists("jsmin")
True
>>> cmd_exists("cssmin")
False
>>> cmd_exists("ls")
True
>>> cmd_exists("dir")
False
>>> cmd_exists("node")
True
>>> cmd_exists("steam")
False
查看更多
登录 后发表回答