等同于Python中的Bash反引号 [英] Equivalent of Bash Backticks in Python

查看:90
本文介绍了等同于Python中的Bash反引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相当于Ruby和Perl在Python中发现的反引号是什么?也就是说,在Ruby中,我可以这样做:

What is the equivalent of the backticks found in Ruby and Perl in Python? That is, in Ruby I can do this:

foo = `cat /tmp/baz`

Python中的等效语句是什么样的?我已经尝试过 os.system( cat / tmp / baz),但这会将结果列为标准值,并向我返回该操作的错误代码。

What does the equivalent statement look like in Python? I've tried os.system("cat /tmp/baz") but that puts the result to standard out and returns to me the error code of that operation.

推荐答案

output = os.popen('cat /tmp/baz').read()

这篇关于等同于Python中的Bash反引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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