How to do multiple arguments with Python Popen?

To directly answer your question, read below. But there’s a lot of problems with your program, some of which I cover in “Better practice.”


By default, subprocess.Popen commands are supplied as a list of strings.

However, you can also you can use the shell argument to execute a command “formatted exactly as it would be when typed at the shell prompt.”

No:

>>> p = Popen("cat -n file1 file2")

Yes:

>>> p = Popen("cat -n file1 file2", shell=True)
>>> p = Popen(["cat", "-n", "file1", "file2"])

There are a number of differences between these two options, and valid use cases for each. I won’t attempt to summarize the differences- the Popen docs already do an excellent job of that.


So, in the case of your commands, you’d do something like this:

cmd = "gnome-terminal -x sudo git clone git://github.com/pererinha/gedit-snippet-jquery.git && sudo cp -f gedit-snippet-jquery/js.xml /usr/share/gedit/plugins/snippets/js.xml && sudo rm -rf gedit-snippet-jquery"
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
          close_fds=False)

Better practice

However, using Python as a wrapper for many system commands is not really a good idea. At the very least, you should be breaking up your commands into separate Popens, so that non-zero exits can be handled adequately. In reality, this script seems like it’d be much better suited as a shell script. But if you insist on Python, there are better practices.

The os module should take the place of calls to rm and cp. And while I have no experience with it, you might want to look at tools like GitPython to interact with Git repositories.

Compatibility concerns

Lastly, you should be careful about making calls to gnome-terminal and sudo. Not all GNU/Linux users run Ubuntu, and not everyone has sudo, or the GNOME terminal emulator installed. In its current form, your script will crash, rather unhelpfully, if:

  • The sudo command is not installed
  • The user is not in the sudoers group
  • The user doesn’t use GNOME, or its default terminal emulator
  • Git is not installed

If you’re willing to assume your users are running Ubuntu, calling x-terminal-emulator is a much better option than calling gnome-terminal directly, as it will call whatever terminal emulator they’ve installed (e.g. xfce4-terminal for users of Xubuntu).

Leave a Comment