0
点赞
收藏
分享

微信扫一扫

Read Output From Shell

杨沐涵 2022-11-09 阅读 122


Python provides a lot of method to read output from a just executed shell. However many of them has been deprecated(Not recommened). But subprocess works at present compared to other methods.



from subprocess import Popen,PIPE,STDOUT

def readFromCommand(command) :
p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
result = p.stdout.read().strip()
return result

print readFromCommand('ls')
#result
#0001-.patch
#0001-.patch.zip
#0001-Replace-app_name-into-Browser.patch

A detailed description about subprocess has been written down here. ​​http://docs.python.org/2/library/subprocess.html​​

Others

  • Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython


举报

相关推荐

0 条评论