【问题标题】:Popen shell commands and problems with spacesPopen shell 命令和空格问题
【发布时间】:2016-10-14 14:44:51
【问题描述】:

我有这个代码,它运行用户为 adb 输入的任何命令,例如,用户输入单词“设备”,“adb.exe 设备”将运行并打印出设备列表。 这适用于“设备”,但只要发出更复杂的命令,例如带有空格的命令,“shell pm path com.myapp.app”就会失败。

       c_arg = self.cmdTxt.GetValue() ##gets the user input, a string
       params = [toolsDir + '\\adb.exe', c_arg] ##builds the command
       p = Popen(params, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)  ## executes the command
       stdout, stderr = p.communicate()  ## get output
       self.progressBox.AppendText(stdout) # print output of command

在将 .GetValue() 中的字符串放入 params 并在 Popen 中运行之前,是否需要对字符串进行一些格式化或处理?

【问题讨论】:

  • 如果您提供命令行作为吃的列表,shell 必须是 False - 默认值,所以只需删除 shell 论据

标签: android python shell wxwidgets


【解决方案1】:

subprocess.Popen with shell=True 采用完整的命令字符串,而不是列表。

params = str(buildpath)+"\\adb.exe"+c_arg #c_arg is a string of arguments.

【讨论】:

  • 我还有很多其他的 Popen 实例,例如 params = [toolsDir + "\\adb.exe", "shell", "pm", "path", app],它们都可以正常工作。
  • c_arg的格式是从self.cmdTxt.GetValue()获取的字符串
  • 所以 c_arg 会像:“shell pm path app”?
  • 是的,完全正确,或者可以是任何其他 adb 命令,例如 pull /sdcard/myfile c:\mylocaldirectory adb.exe 已在 params 行中定义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多