Question : os.system and shell aliases

I am trying to write a python script in Linux that will execute a series of shell commands when run by a user.  I am running os.system to do this but I noticed one problem.  I need to pickup a user's aliases and the os.system call does not seem to be doing this.  Can I get os.system to do it or do I need to use a different mehtod?  I thought about subprocess but I need to execute a couple of lines at a time so I pass one big string with semi-colons as seperators.

Answer : os.system and shell aliases

If I am not mistaken, the shell is responsible for the aliases. This way, you have to ensure that Python does the things through the shell. You probably should try the newer module called subprocess (http://docs.python.org/lib/module-subprocess.html). There is a section about how to replace the older os.system(). You can notice that there is the optional argument shell=True (or False). However, the module was introduced only in Python 2.4 and some functionality in 2.5.

See how os.system() should be replaced (http://docs.python.org/lib/node536.html). Notice that subprocess.Popen() is the core of the new replacement. Notice the usage of the shell argument.

There also is a convenicence function subprocess.call() that calls the Popen() plus waits to the result -- you may want it.

You can pass or a sequence of the arguments (list or tuple) or the string. This way you should be able to replace your os.system() almost mechanically by subprocess.call().

I am curious if it is going to solve the problem or not. However, my guess is that if there is a difference then it is only because subprocess is newer (implemented better).
Random Solutions  
 
programming4us programming4us