Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *