top of page
Search
vinovnica6232

Python os.system() os.popen(): Best Practices and Common Pitfalls for Using These Functions



Is it possible to use os.popen() to achieve a result similar to os.system? I know that os.popen() is more secure, but I want to know how to be able to actually run the commands through this function. When using os.system(), things can get very insecure and I want to be able to have a secure way of accessing terminal commands.




Python os.system() os.popen()



While making a program in python, you may need to exeucte some shell commands for your program. For example, if you use Pycharm IDE, you may notice that there is option to share your project on github. And you probably know that file transferring is done by git, which is operated using command line. So, Pycharm executes some shell commands in background to do it. However, In this tutorial we will learn some basics about executing shell commands from your python code.


So far, we executed the system commands with the help of python. But we could not manipulate the output produced by those commands. Using subprocess.check_output() function we can store the output in a variable.


So, in the above sections we have discussed about basic ideas about executing python system command. But there is no limit in learning. If you wish, you can learn more about Python System command using subprocess module from official documentation.


os.popen() does the same thing as os.system except that it gives us a file-like stream object that we can use to access standard input/output for that process. There are 3 other variants of popen that all handle the i/o slightly differently.


This is intended as a replacement for os.popen, but it is more complicated. For example, we usesubprocess.Popen("echo Hello World", stdout=subprocess.PIPE, shell=True).stdout.read()instead ofos.popen("echo Hello World").read()But it is comprehensive and it has all of the options in one unified class instead of different os.popen functions.


4. os.close(): Close file descriptor fd. A file opened using open(), can be closed by close()only. But file opened through os.popen(), can be closed with close() or os.close(). If we try closing a file opened with open(), using os.close(), Python would throw TypeError.


You have seen now how to run external commands in Python. The most effective way is to use the subprocess module with all the functionality it offers. Most notably, you should consider using subprocess.run. For a short and quick script you might just want to use the os.system() or os.popen() functions. If you have any questions, feel free to leave them in the comments below. There are also other useful libraries that support shell commands in Python, like plumbum, sh, psutils and pexpect.


The second way to run Linux commands with Python is by using the newer subprocess module. This module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. It was created to replace both os.system() and os.popen() functions.


script = "f:\\scratch\\test.py"pythonExePath = r'C:\Python27\ArcGISx6410.8\python.exe'pid = subprocess.Popen([pythonExePath, script, 'file_from_subprocess'], shell=False) # Call subprocess


Furthermore, using the same media player example, we can see how to launch theSubprocess in python using the popen function. You can start the Windows Media Player as a subprocess for a parent process.


You can imagine the intended use case is to wrap ls and add something to it. So the expected user behavior is to provide a path like "/home/realpython/". However, if a malicious actor realized what was happening, they could execute almost any code they wanted. Take the following, for instance, but be careful with this:


Here the system function of the os module allows us to check that we can execute commands, but the result of the command is not reflected in the generated template (only the return code is shown). To return the result of the command, we need to use os.popen(command).read() like this:


os.system()*os.spawn()os.popen()popen2.()commands.()According to the python official documentation, the subprocess module will rule these previously stated functions as they are going to be deprecated very soon.


POSIX users (Linux, BSD, etc.) are strongly encouraged to installand use the much more recent subprocess32 module instead of theversion included with python 2.7. It is a drop in replacement withbetter behavior in many situations.


Python subprocess is a module in python that helps us to execute shell commands, get the input/output/error pipes, and the error codes for the executed command from the python language.


The subprocess.run() function was added in Python 3.5 and it is recommended to use the run() function to execute the shell commands in the python program.


Also note that all popen functions are deprecated since python2.6 and should have been avoided since then. The subprocess module provides a much better interface to launching subprocesses which is shellshock safe by default. If you don't pass the shell=True argument your program wont launch subshells and hence wont be affected by shellshock.


The os.popen() will behave the same as os.system function, however, instead of returning the return code, it returns a file-like object which can be used to access the standard input/output for the process being executed. The popen command allows us to interact with the system and create a pipe to or from another command. It has four different variants which are: 2ff7e9595c


1 view0 comments

Recent Posts

See All

Comments


bottom of page