15

I am currently executing bash commands manually, by entering in shell in the python code.

How would one do this in the pythonic way?

i am currently using os.system function to execute commands like;

os.system('sudo add-apt-repository ppa:ondrej/php')
os.system('sudo apt-get update')
os.system('sudo apt-get install php7.0 php5.6 php5.6-mysql php-gettext php5.6-mbstring php-mbstring php7.0-mbstring php-xdebug libapache2-mod-php5.6 libapache2-mod-php7.0')
os.system('sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart')
2

1 Answer 1

27

Possible duplicate of this question.

It is recommended to use subprocess module instead. os.system has been depreciated in favour of subprocess. For more information see subprocess documentation.

import subprocess

command = 'sudo add-apt-repository ppa:ondrej/php'
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
Sign up to request clarification or add additional context in comments.

3 Comments

An response like this needs to go in as comment.
@Anil_M oh, ok sorry
There's also the convience function in python 3 for getstatusoutput which is pretty nice. ` status, output = subprocess.getstatusoutput('cmd')`

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.