The os module provides an easy way to interact with files, directories and other parts of your operating system. It contains many functions to list, change, copy, remove and examine files and directories.
import os
print(os.path.exists('README.rst'))os does these as well:
import os
os.system('python -c "print(6*7)"')
os.remove('copy.md')The following code prints all Python files in a given folder:
import os
for filename in os.listdir("/home/participant/"):
if filename.endswith(".py"):
print(filename)A very simple way to start other processes is the os.system() function. The following example starts a program to list contents of a folder and redirects the output to a file:
import os
os.system("ls -l > result.txt")Warning
os.system does not allow for any communication with the running process. If it crashes or runs forever, your Python code won't learn about it. A safer alternative is the subprocess module.
.. seealso::
`Documentation of the os module <https://docs.python.org/3/library/os.html>`__