是否有一个等同于“哪个"命令的Python [英] Is there a Python equivalent to the 'which' command

查看:45
本文介绍了是否有一个等同于“哪个"命令的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用另一种方式,是否有一种跨平台的方式来知道subprocess.Popen(file)将在不先执行的情况下执行哪个文件?

Put another way, is there a cross-platform way of knowing which file will be executed by subprocess.Popen(file) without first executing it?

推荐答案

我相信python库中没有

I believe there is none in the python libraries

>>> def which(pgm):
    path=os.getenv('PATH')
    for p in path.split(os.path.pathsep):
        p=os.path.join(p,pgm)
        if os.path.exists(p) and os.access(p,os.X_OK):
            return p


>>> os.which=which
>>> os.which('ls.exe')
'C:\\GNUwin32\\bin\\ls.exe'

这篇关于是否有一个等同于“哪个"命令的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆