我正在尝试使用环境变量将一些秘密传递给 shell 命令。Popen 似乎使用“单引号概念”来转义参数,因此命令无法获取变量值。
Python 2.7.1 (r271:86832, Mar 3 2017, 10:25:58)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux3
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.Popen(['echo','$PATH'])
$PATH
预期输出是真实的 $PATH 值,如“/bin:/sbin:/usr/bin...”
谢谢!
您需要一个 shell 来评估您的行:
subprocess.Popen('echo $PATH', shell=True)
但如果这就是你想要的,那么考虑一下:
print os.getenv('PATH')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句