Shell 如何在python中显示命令的输出

Shell 如何在python中显示命令的输出,shell,command,python-3.3,Shell,Command,Python 3.3,我想运行命令df-h来显示磁盘信息,但当我运行代码时,终端上没有显示任何内容,我甚至尝试了“df-h>out.txt”,然后是cat out.txt,但也不起作用 import sys import os import subprocess def main(): os.system('clear') print("Options: \n") print("1 - Show disk info") print("2 - Quit \n") select

我想运行命令df-h来显示磁盘信息,但当我运行代码时,终端上没有显示任何内容,我甚至尝试了“df-h>out.txt”,然后是cat out.txt,但也不起作用

import sys
import os
import subprocess


def main():
    os.system('clear')
    print("Options: \n")
    print("1 - Show disk info")
    print("2 - Quit \n")
    selection = input('> ')
    if selection == 1:
        subprocess.call(['df -h'], shell=True)
    elif selection == 2:
        sys.exit(0)

if __name__ == "__main__":
    main()

使用
input()
读取用户输入返回一个字符串。但是,您的
if/elif
语句将
选择的内容与整数进行比较,因此比较将始终为
False
。作为解决方法,请使用以下方法:

selection = int(input('> '))
if selection == 1:
   ...

别担心,我们都做过了。这就是为什么交互开发如此重要的原因-运行
类型(选择)
甚至
打印(选择)
将向您展示您需要了解的内容。