【问题标题】:Any way to input a command and execute it? [duplicate]有什么方法可以输入命令并执行它? [复制]
【发布时间】:2021-12-09 02:07:39
【问题描述】:
a = input('Enter something:') #e.g. input is print('Hello World')
# some function
print(a) #and then we get only `Hello World` out as we use print

我已经研究过通过 python 执行 shell 命令,但与我想做的事情无关。我需要更改为不是 python 的语言吗?有可能吗?

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    对于语句,请使用 exec(string) (Python 2/3) 或 exec string (Python 2):

    所以不要打印a,而是使用exec

    顺便说一句,如果您打算在任何软件中按原样使用它,我会说这是高度不安全

    【讨论】:

      【解决方案2】:

      你可以使用exec:

      >>> a = input("Enter something: ")
      Enter something: print("hello world")
      >>> exec(a)
      hello world
      

      【讨论】:

        猜你喜欢
        • 2016-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-18
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 2020-07-01
        相关资源
        最近更新 更多