python logo

Python Commandments

Python system command

The os.system() function runs commands on the system, this creates a new process. Using the system function, the child process cannot affect the environment variables in the parent process.

To put it simply, every operation of os.system() opens a child process, which returns the parent process when it is finished, but cannot change the environment variable of the parent process.

python run command

The call os module has two functions for command execution: os.system() and os.popen(). These two functions need os this library, before using the import os

The difference between the two functions:

  • os.system() is the state code that is returned after the execution result is executed in the Shell, with 0 indicating a successful execution.

  • os.popen() is the direct return of the execution result, which returns a memory address and parses the data in the memory address with read(). (PS: address or object, C programmers understand address better)

os.system() example

By calling os.system() you can execute a system command, but it does not return the output to Python. This will run a command on your system.

On Linux and Mac the command pwd returns the current directory, but any command will do. You can also use os.system() to start a program.

>>> import os
>>> os.system('pwd')
/home/frank/Pictures
0
>>> os.system('ls')
800px-Fc7-wallpaper.png.jpeg                      Fedora-16-Wallpaper.png                       primark-pink-fedora.jpg  test.py
9dd027cc6a42f494c219855b04a6f3ee-700.jpg          fedora.jpg                                    python-vs-golang.png     test.py~
babkinstas-P1430889.JPG                           gnokii-william-bout-7cdFZmLlWOM-unsplash.jpg  ronvau-IMG_4719.JPG      vladoxncl-Groyne_Fedora.jpg
darthcookie-juskteez-vu-mwhklqGVzck-unsplash.jpg  output.png                                    test.png
0
>>> # output is not stored into variable
>>> n = os.system('ls')
800px-Fc7-wallpaper.png.jpeg                      Fedora-16-Wallpaper.png                       primark-pink-fedora.jpg  test.py
9dd027cc6a42f494c219855b04a6f3ee-700.jpg          fedora.jpg                                    python-vs-golang.png     test.py~
babkinstas-P1430889.JPG                           gnokii-william-bout-7cdFZmLlWOM-unsplash.jpg  ronvau-IMG_4719.JPG      vladoxncl-Groyne_Fedora.jpg
darthcookie-juskteez-vu-mwhklqGVzck-unsplash.jpg  output.png                                    test.png
>>> n
0

os.popen() example

If you call the popen() method it returns the output to Python in the form of a string. The string contains many strings that have the newline character \n.

>>> 
>>> # can use os.popen to store output
>>> n = os.popen('ls')
>>> n = n.read()
>>> n
'800px-Fc7-wallpaper.png.jpeg\n9dd027cc6a42f494c219855b04a6f3ee-700.jpg\nbabkinstas-P1430889.JPG\ndarthcookie-juskteez-vu-mwhklqGVzck-unsplash.jpg\nFedora-16-Wallpaper.png\nfedora.jpg\ngnokii-william-bout-7cdFZmLlWOM-unsplash.jpg\noutput.png\nprimark-pink-fedora.jpg\npython-vs-golang.png\nronvau-IMG_4719.JPG\ntest.png\ntest.py\ntest.py~\nvladoxncl-Groyne_Fedora.jpg\n'
>>> 
python system command

By calling the .split('\n') method you can turn the string into a list.

>>> lines = n.split('\n')
>>> lines
['800px-Fc7-wallpaper.png.jpeg', '9dd027cc6a42f494c219855b04a6f3ee-700.jpg', 'babkinstas-P1430889.JPG', 'darthcookie-juskteez-vu-mwhklqGVzck-unsplash.jpg', 'Fedora-16-Wallpaper.png', 'fedora.jpg', 'gnokii-william-bout-7cdFZmLlWOM-unsplash.jpg', 'output.png', 'primark-pink-fedora.jpg', 'python-vs-golang.png', 'ronvau-IMG_4719.JPG', 'test.png', 'test.py', 'test.py~', 'vladoxncl-Groyne_Fedora.jpg', '']
>>> 

So unlike the system() function, the popen function lets you use the output of the commmand in your Python program. For portability issues, make sure the command is available on all desired platforms.