Menu Close

How to escape os.system() calls with Python?

Sometimes, we want to escape os.system() calls with Python.

In this article, we’ll look at how to escape os.system() calls with Python.

How to escape os.system() calls with Python?

To escape os.system() calls with Python, we can use shlex.quote.

For instance, we write

from shlex import quote
command = 'ls -l {}'.format(quote(filename))

to call quote with filename to escape the filename path.

shlex.quote can be used with Linux only.

Conclusion

To escape os.system() calls with Python, we can use shlex.quote.

Posted in Python, Python Answers