Search code examples
python-3.xsubprocessncurses

How can i run multiple commands in python script


How can i run multiple commands using python script

I have already written following code, but still is there any other alternative?

 os.system("clear")
 os.system("virtualenv -p python3 /opt/"+name)
 os.system("source /opt/"+name+"/bin/activate")
 os.system("pip install /usr/share/my-packages/*")

Also please let me know if there is any way that can do one of following: -Either don't on console what is happening with commands such as: creating virtual envirnoment. etc. -Or else those statements which are going to be printed on console any how i can get it in any variable so that i can use those on python-curses.


Solution

  • Try building a list:

    commands = ['clear', 
                'virtualenv -p python3 /opt/'+name, 
                'source /opt/'+name+'/bin/activate',
                'pip install /usr/share/my-packages/*']
    
    for command in commands:
        os.system(command)
    

    This allows for easier maintenance of the commands you want to include/exclude.