当cmd是文本变量时运行subprocess.call(cmd)

2024-09-22 16:29:53 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中,当命令以文本形式存储在变量中时,是否可以运行shell命令? 例如:

  self.command_editor = QTextEditor()
  self.command_editor.append("echo command")
  cmd = str(self.command_editor.toPlainText())
  call(cmd)

此代码无效


Tags: 代码文本命令echoselfcmdshellcall
1条回答
网友
1楼 · 发布于 2024-09-22 16:29:53

这是正确的答案:

  self.command_editor = QTextEditor()
  self.command_editor.append("echo command")
  cmd = str(self.command_editor.toPlainText())
  call(cmd, shell = True) #the mistake was here

相关问题 更多 >