2016-12-16 87 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 

相关问题