ayaan
ayaan

Reputation: 735

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. so i used subprocess.Popen like this:

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

This works fine but now i want to choose a file of my choice by browsing using askfilename() func from tkFileDailog() module. 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

Upvotes: 1

Views: 13451

Answers (2)

Rikka
Rikka

Reputation: 1049

Because if you use shell=True, you can only use one string as the first argument.

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).

Upvotes: 1

jnrbsn
jnrbsn

Reputation: 2533

If you pass shell=True, the first argument should be a string, and it will be interpreted as the shell would interpret it. If shell=False, then the first argument should be a list, which sort of bypasses the shell.

Here's an answer to another question that explains it well: https://stackoverflow.com/a/15109975/451201

Upvotes: 3

Related Questions