A module called os lets you interact with the operating system using Python. It provides functions that are available on Unix and/or Windows systems. If you’re familiar with the command console, you’ll see that some functions give the same results as the commands available on the operating systems.
1. The uname() function returns an object that contains information about the current operating system. The object has the following attributes:
-
- systemname (stores the name of the operating system)
- nodename (stores the machine name on the network)
- release (stores the operating system release)
- version (stores the operating system version)
- machine (stores the hardware identifier, e.g. x86_64.)
|
1 2 |
import os print(os.uname()) |
Unfortunately, the uname() function only works on some Unix systems. If you use Windows, you can use the uname function in the platform module, which returns a similar result.
2. The name attribute available in the os module allows you to distinguish the operating system. It returns one of the following three values:
-
- posix (you’ll get this name if you use Unix)
- nt (you’ll get this name if you use Windows)
- java (you’ll get this name if your code is written in something like Jython)
|
1 2 |
import os print(os.name) |
3. The mkdir() function creates a directory in the path passed as its argument. The path can be either relative or absolute, e.g:
|
1 2 3 4 |
import os os.mkdir("hello") # the relative path os.mkdir("/home/python/hello") # the absolute path |
Note: If the directory exists, a FileExistsError exception will be thrown.
To change the directory permissions, you can use the chmod() function, which works similarly to the chmod command on Unix systems.
4. The makedirs() function enables recursive directory creation, which means that all directories in the path will be created.
|
1 2 3 4 5 |
import os os.makedirs("my_first_directory/my_second_directory") os.chdir("my_first_directory") print(os.listdir()) |
NOTE: The equivalent of the makedirs function on Unix systems is the mkdir command with the -p flag, while in Windows, simply the mkdir command with the path:
-
- Unix-like systems:
mkdir -p my_first_directory/my_second_directory
- Unix-like systems:
-
- Windows:
mkdir my_first_directory/my_second_directory
- Windows:
5. The result of the listdir() function is a list containing the names of the files and directories that are in the path passed as its argument.
It’s important to remember that the listdir() function omits the entries ‘.’ and ‘..’, which are displayed, for example, when using the ls -a command on Unix systems. If the path isn’t passed, the result will be returned for the current working directory.
|
1 2 3 4 |
import os os.mkdir("my_first_directory") print(os.listdir()) |
6. To move between directories, you can use a function called chdir(), which changes the current working directory to the specified path. As its argument, it takes any relative or absolute path.
7. If you want to find out what the current working directory is, you can use the getcwd() function, which returns the path to it.
|
1 2 3 4 5 6 7 |
import os os.makedirs("my_first_directory/my_second_directory") os.chdir("my_first_directory") print(os.getcwd()) os.chdir("my_second_directory") print(os.getcwd()) |
NOTE: On Unix-like systems, the equivalent of the getcwd function is the pwd command, which prints the name of the current working directory.
8. To remove a directory, you can use the rmdir() function.
|
1 2 3 4 5 6 |
import os os.mkdir("my_first_directory") print(os.listdir()) os.rmdir("my_first_directory") print(os.listdir()) |
To remove a directory and its subdirectories, use the removedirs() function.
|
1 2 3 4 5 |
import os os.makedirs("my_first_directory/my_second_directory") os.removedirs("my_first_directory/my_second_directory") print(os.listdir()) |
9. On both Unix and Windows, you can use the system() function, which executes a command passed to it as a string, e.g.:
|
1 2 3 |
import os returned_value = os.system("mkdir hello") |
The system function on Windows returns the value returned by shell after running the command given, while on Unix it returns the exit status of the process. Exit status 0 indicates success on Unix systems.
Exercise 1
What is the output of the following snippet if you run it on Unix?
|
1 2 |
import os print(os.name) |
posix
Exercise 2
What is the output of the following snippet?
|
1 2 3 4 |
import os os.mkdir("hello") print(os.listdir()) |
['hello']

