os
Module#
The os
module, short for Operating System, offers a portable way of interacting with various operating system-dependent functionalities. Offical Documentation for the OS module
Importing the os Module#
Before we dive into the various capabilities of the os module, we need to import it into our Python script. This is done using a straightforward import statement:
import os
print(os.listdir('.'))
['os.ipynb', 'subprocess.ipynb', 'sqlite3.ipynb', 'webbrowser.ipynb', 'datetime.ipynb', 'logging.ipynb', 'errors.log', 'index.ipynb', 'statistics.ipynb']
With this import statement, we gain access to a multitude of methods and functionalities that allow us to interact with the operating system in a platform-independent manner.
Working with Directories#
Creating Directories#
The os module empowers you to create directories for organizing files. Two essential functions for this purpose are os.mkdir('<path>')
and os.makedirs('<path>')
. The key distinction between them is that mkdir
can only create a directory if its parent directory already exists, whereas makedirs
will create the entire parent structure as needed:
import os
os.mkdir('./myNewDirectory/') # This will work if the parent directory exists
os.makedirs('./newParentDirectory/myNewDirectory') # This will succeed, creating parent directories as required
Both os.mkdir()
and os.makdirs
will fail if the directory already exists (a very common scenario when running a program that generates logs).
Note
Use os.makedirs()
with caution. It is notorious for creating difficult-to-track logical errors. If the directory structure does not exist, it will be created, even if there is an error in the path. So it would be very easy to create ~/newParentDirectry/myNewDirectory
(note the missing “o” in “Directory”).
Removing Directories#
Getting rid of empty directories is also possible. The os.rmdir()
and os.removedirs()
functions work in the same way as making directories above - the os.rmdir
will only remove one directory, wherease the os.removedirs()
will remove all the parent structure as well. For example:
os.rmdir('./myNewDirectory') # will only remove target directory
# assuming current working directory is /home/prog1700/Desktop/myNewDirectory'
os.removedirs('./newParentDirectory/myNewDirectory') # will remove all empty directories in the path
Note
Both `os.rmdir()` and `os.removedirs()` only work with **empty** directories. As soon as a directory contains either a file or another directory, the command will fail (or, in the case of `os.removedirs()` stop recursively walking up the file tree). Therefore, it is possible to use either relative or absolute paths with these functions, as long as the target directories are empty.