简体   繁体   English

subprocess.Popen的命令行参数

[英]command line arguments with subprocess.Popen

I've GUI where if i press a button "process" calls a python script which needs one command line argument. 我有GUI,如果我按下按钮,“进程”将调用需要一个命令行参数的python脚本。 so i used subprocess.Popen like this: 所以我像这样使用subprocess.Popen:

subprocess.Popen(['chbif /home/vnandak/Work/VS30_Cradle/SV000_sv000/cradle_vs30_dkaplus01_fwd_dl140606_fem140704_v00_mod1.bif'],shell=True)

chbif is the alias for the .py script chbif是.py脚本的别名

This works fine but now i want to choose a file of my choice by browsing using askfilename() func from tkFileDailog() module. 这工作正常,但现在我想通过使用tkFileDailog()模块中的askfilename()函数浏览来选择所需的文件。 how can i do this? 我怎样才能做到这一点?

I thought of doing it like this: 我想这样做:

def bifcall():
        name13= askopenfilename()
        subprocess.Popen(['chbif', name13],shell=True)

But if i use this it throws an error saying that it does'nt have one more command line argument which is the file name 但是如果我使用它,它会抛出一个错误,说它没有另一个命令行参数,即文件名

If you pass shell=True , the first argument should be a string, and it will be interpreted as the shell would interpret it. 如果传递shell=True ,则第一个参数应该是字符串,并且它将被解释为shell会解释它。 If shell=False , then the first argument should be a list, which sort of bypasses the shell. 如果shell=False ,则第一个参数应为列表,该列表绕过shell。

Here's an answer to another question that explains it well: https://stackoverflow.com/a/15109975/451201 这是一个很好地解释了另一个问题的答案: https : //stackoverflow.com/a/15109975/451201

Because if you use shell=True, you can only use one string as the first argument. 因为如果使用shell = True,则只能使用一个字符串作为第一个参数。

Try to change 尝试改变

subprocess.Popen(['chbif', name13],shell=True)

to

subprocess.Popen('chbif ' + name13,shell=True)

But it is not recommended to use shell=True for security reasons (See https://docs.python.org/2/library/subprocess.html#frequently-used-arguments for details). 但是出于安全原因,不建议使用shell = True(有关详细信息,请参见https://docs.python.org/2/library/subprocess.html#frequently-used-arguments )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM