【问题标题】:Python 3.5: Passing a string as parameters to the subprocess procedurePython 3.5:将字符串作为参数传递给子进程过程
【发布时间】:2019-07-19 12:57:35
【问题描述】:

我在 rasbian pi linux 系统上使用 python 3.5。我还是新手,但确实有一些 vba 编码经验。

我的问题是这个。以下代码行正常工作:

#working
import subprocess
chrome = "chromium-browser"
site="www.ebay.com.au"
proc=subprocess.Popen([chrome,site],stdout=subprocess.PIPE)
leaf1="leafpad"
leaf2="--display"
leaf3=":0.0"
leaf4="/home/pi/Documents/leaftxt.txt"
proc=subprocess.Popen([leaf1,leaf2,leaf3,leaf4],stdout=subprocess.PIPE)

此代码成功将 Chrome 打开到 ebay,然后是一个名为 leafpad 的文本编辑器,其中打开了文本文件 leaftxt.txt。

但是当我尝试从文本文件中加载参数字符串时,我得到一个错误:

#not working
import subprocess
tasks="/home/pi/Documents/tasklist.txt"
try:
    f=open(tasks,"r")
except FileNotFoundError:
    print('File Not found.')
    sys.exit()
for x in f:
    x1=x.strip('\n')
    proc=subprocess.Popen([x1],stdout=subprocess.PIPE)

出现的错误如下:

    Traceback (most recent call last):    
    File "/home/pi/Documents/P3Scripts/test7.py", line 19, in <module>      
proc=subprocess.Popen([x1],stdout=subprocess.PIPE)    
    File "/usr/lib/python3.5/subprocess.py", line 676, in __init__      
restore_signals, start_new_session)   
    File "/usr/lib/python3.5/subprocess.py", line 1282, in _execute_child       
raise child_exception_type(errno_num, err_msg)  
    FileNotFoundError: [Errno 2] No such file or directory: 'chromium-browser, www.ebay.com.au'

tasklist.txt 包含的文本文件(我也试过不带逗号)

chromium-browser, www.ebay.com.au
leafpad, --display, :0.0, /home/pi/Documents/leaftxt.txt

这两个文件似乎都在做同样的事情,但我在参数的格式中遗漏了一些东西,因为它们在第二个子流程过程调用中使用。

我错过了什么/做错了什么?谢谢。

【问题讨论】:

    标签: python-3.x subprocess parameter-passing


    【解决方案1】:

    有区别

    Popen(["foo", "bar"])        # correct: you parse arguments
    

    Popen("foo bar", shell=True) # correct on POSIX: shell parses arguments
    

    Popen(["foo, bar"])          # incorrect: noone parses arguments
    

    在您的第一个 sn-p 中,您使用第一种形式:程序名称和每个参数作为列表的单独元素。

    在你的第二个sn-p中,你使用了第三种形式:因为你使用的是数组,Popen认为你已经将参数分开了,第一个参数的全部是要执行的程序的名称.当然,名为chromium-browser, www.ebay.com.au 的程序是不存在的。

    【讨论】:

      【解决方案2】:

      在您的第一个示例中,您传递了四个字符串,而在您的第二个示例中,一个字符串包含所有四个。

      你应该把它分开:

      x1=x.strip('\n').split(', ')
      

      【讨论】:

      • 当我这样做时,我收到一条错误消息 - ë期望字节或 str,而不是列表
      • 你还在用[x1],还是只用x1?如果你使用[x1],那么 Popen 会得到一个双重列表:[["chromium-browser", "www.ebay.com.au"]],并抱怨它的第一个元素不是字符串。
      • 做到了。感谢您的帮助 shx2!
      【解决方案3】:

      你可以试试:

      import subprocess
      tasks="/home/pi/Documents/tasklist.txt"
      try:
          f=open(tasks,"r")
      except FileNotFoundError:
          print('File Not found.')
          sys.exit()
      for x in f:
          x1=x.strip('\n').split(", ") #split_str_list is a list that contains string of single line in /home/pi/Documents/tasklist.txt
          proc=subprocess.Popen(x1,stdout=subprocess.PIPE)
      

      您传递的字符串包含用逗号分隔的参数。 Popen 不接受。
      Popen args 应该是一个必须用空格或参数序列分隔的字符串。

      https://docs.python.org/3/library/subprocess.html#subprocess.Popen

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-27
        • 2016-07-21
        • 1970-01-01
        • 1970-01-01
        • 2016-06-29
        • 1970-01-01
        • 2011-01-10
        相关资源
        最近更新 更多