相关疑难解决方法(0)

运行shell命令并捕获输出

我想编写一个函数来执行shell命令并将其输出作为字符串返回,无论是错误还是成功消息.我只想获得与命令行相同的结果.

什么是代码示例会做这样的事情?

例如:

def run_command(cmd):
    # ??????

print run_command('mysqladmin create test -uroot -pmysqladmin12')
# Should output something like:
# mysqladmin: CREATE DATABASE failed; error: 'Can't create database 'test'; database exists'
Run Code Online (Sandbox Code Playgroud)

python shell subprocess

816
推荐指数
15
解决办法
96万
查看次数

如何从 os.system() 获取输出?

我想从中获取输出,os.system("nslookup google.com")0在打印时我总是得到, 。这是为什么,我该如何解决?(Python 3,Mac)

(我查看了如何存储它在 python 中打印到 stdout 的 os.system 的返回值? -但我不明白~我是 python 新手)

python subprocess os.system python-3.x

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×2

subprocess ×2

os.system ×1

python-3.x ×1

shell ×1