【问题标题】:How to get output of OS command from Jupyter notebook?如何从 Jupyter 笔记本获取操作系统命令的输出?
【发布时间】:2019-09-28 10:36:16
【问题描述】:

我在服务器 (python 3) 上运行 Jupyter notebook。

想要查看操作系统命令的输出(任何操作系统命令 - 这只是示例):

output = os.system("pwd")

当我打印它时:

print (output)

回复是0

如何获得简单的输出(如在 CLI 中)?

谢谢。

【问题讨论】:

标签: python linux operating-system jupyter


【解决方案1】:

刚在网上找到,想发帖。

必须是:

print(os.popen('ls').read())

(或任何其他操作系统命令)。

这很好用。

【讨论】:

    【解决方案2】:
    import os
    
    print(os.getcwd())
    print(os.system("pwd"))
    

    但是这个问题是重复的: how to show current directory in ipython promp

    【讨论】:

    • print(os.system("pwd")) 回复为0
    • print(os.getcwd()) 工作正常(可以看到预期的输出)。只需要os.system 响应(而不是0)..
    • 是的,我添加了 os.system 以显示与 os.getcwd 相比的输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 2010-12-25
    • 2022-01-03
    相关资源
    最近更新 更多