【问题标题】:os.system() doesnt work [Ubuntu 16.04], [Python 3.5.2]os.system() 不起作用 [Ubuntu 16.04]、[Python 3.5.2]
【发布时间】:2017-02-21 23:04:54
【问题描述】:

下面是代码。当我在 PyCharm(我的编辑器)中运行它时,一切正常,但是当我在终端中运行 python 脚本然后输入数字时,除了“完成!!”之外什么都没有发生印刷。是的,我正在使用sudo 运行脚本。

# -*- coding: utf-8 -*-
#!usr/bin/python
import os
import time


print('==========================')
print('==========================')
print('1. Update System')
print('2. Check IP-Address')
print('3. Train')
print('==========================')
print('==========================')

Nummer = input('What do you want to do?: ')


def nummer1():
    if Nummer == '1':
        print('System-Update is starting...')
        time.sleep(2)
        os.system("gnome-terminal -e 'sudo apt-get update'")
nummer1()

def nummer2():
     if Nummer == '2':
        print('Checking IP-Address...')
        time.sleep(2)
        os.system("gnome-terminal -e 'sudo ifconfig'")
nummer2()

def nummer3():
    if Nummer == '3':
        os.system("gnome-terminal -e 'sudo apt-get install sl && sl'")
        time.sleep(1)
        print('Get ready...')
        time.sleep(2)
nummer3()
print('Finished!!')

【问题讨论】:

  • 我猜,你正在从终端运行 Python 2 并且 input() 被评估为一个整数,所以你的 if 语句失败
  • 在哪里可以看到这个以及如何更改这个?
  • 将输入显式转换为strint 并进行适当比较。
  • 更好:在终端上使用python3 在两种情况下都使用相同的可执行文件运行文件。
  • 使用 raw_input 代替 input。

标签: python ubuntu menu operating-system


【解决方案1】:

使用 python 2 您将输入作为整数,因此它会失败。

更改输入行:

Nummer = str(input('What do you want to do?: '))

Nummer = raw_input('What do you want to do?: ')

但是如果你像这样运行你的代码

python3 文件名.py

它会起作用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-11
    • 2017-04-03
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多