2016-12-16 86 views
0

我想將一個字符串作爲System Argument變量傳遞給python的gnuplot。我之前做了幾次,但令人驚訝的是這次並不奏效。我用這個主題How to pass command line argument to gnuplot?,但我沒有工作將字符串作爲系統參數變量傳遞給python的gnuplot

import subprocess 
ii=2 
while ii<5: 
    if (ii==2): 
      name='rectangular' 
      a="gnuplot -e 'name="+name+ "' graph3.gp" 
    if (ii==3): 
      name='trapezoidal' 
    if (ii==4): 
      name='simpson' 

    a="gnuplot -e 'name="+str(simpson)+ "' graph3.gp" 
    subprocess.call(a, shell='true') 
    ii=ii+1 

我總是得到同樣的錯誤信息:

line 0: undefined variable: rectangular 

line 0: undefined variable: trapezoidal 

line 0: undefined variable: simpson 
+0

使用殼=真可以是一個安全[危險](https://docs.python.org/2/library/subprocess.html#常用參數) – Praveen

+0

爲什麼?這怎麼會導致我的代碼中存在的問題?你會這樣做嗎? – anonymous

回答

0

兩件事情:

  1. 調用需要ARGS
  2. 列表
  3. shell需要一個布爾值(True不是'true')
  4. 你可能有一個類型試圖投下「辛普森」作爲一個字符串而不是名字?

也許是這樣的:

subprocess.call(a.split(), shell=True) # or 
subprocess.call(["gnuplot", "-e", "'name={}".format(str(name)), "graph3.gp"], shell=True) 
+0

你的道具沒有任何工作。試着用「辛普森」而不是名字,給了我同樣的錯誤。您的第二個道具導致此錯誤: 回溯(最近呼叫最後): 文件「Plots.py」,第11行,在 subprocess.call([「gnuplot」,「-e」,「'name = { }'「。format(str(name))],」graph3.gp「,shell = True) 文件」/usr/lib64/python2.7/subprocess.py「,第522行,致電 返回Popen(* ()) 文件「/usr/lib64/python2.7/subprocess.py」,第658行,在__init__中 raise TypeError(「bufsize必須是整數」) TypeError:bufsize必須是一個整數 – anonymous

+0

我會打印出你傳遞的字符串,然後嘗試在shell中手動運行它,看看它是否工作。我只是給你一些建議。 – Kelvin

0

好吧,我想通了,如何是可以做到的。一切都作爲字符串將被傳遞:

import subprocess 
ii=2 
while ii<5: 
    if (ii==2): 
      name='name="rectangular"' 
    if (ii==3): 
      name='name="trapezoidal"' 
    if (ii==4): 
      name='name="simpson"' 
    a="gnuplot -e {0} graph3.gp".format(name) 
    subprocess.call(a.split(), shell=False) 
    ii=ii+1 

相關問題