Concatenate Bash commands in python

Viewed 269

I want to concatenate several bash commands in a python file. Doing that for one command works:

import os
os.system('ls -l')

However if I dont know how to concatenate with another command like pwd

2 Answers

Use && and so:

import os
os.system('ls -l && pwd')

This will execute pwd on the successful execution of "ls -l"

Use a semicolon if you want to always execute command 2, regardless of whether or not command 1 ran successfully:

import os
os.system('ls -l; pwd')
Related