13

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')
glibdud
  • 7,550
  • 4
  • 27
  • 37
jagmitg
  • 4,236
  • 7
  • 25
  • 59

1 Answers1

26

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()
zwessels
  • 617
  • 1
  • 10
  • 25
Michael
  • 1,170
  • 2
  • 14
  • 35