Skip to content
Advertisement

How can I execute two commands in terminal using Python’s subprocess module?

How can I use the subprocess module (i.e. call, check_call and Popen) to run more than one command?

For instance, lets say I wanted to execute the ls command twice in quick sucession, the following syntax does not work

JavaScript

returns:

JavaScript

Advertisement

Answer

You can use && or ;:

JavaScript

The difference is that in case of && the second command will be executed only if the first one was successful (try false && ls) unlike the ; in which case the command will be executed independently from the first execution.

So, Python code will be:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement