Python comes with a wide range of built-in modules that further enhance its capabilities. One such module is the OS module, which provides functions for interacting with the operating system. This module offers a portable way of using operating system-dependent functionalities, like reading or writing to the file system. In this topic, you will learn more about the OS module, such as accessing and removing directories and files in the file system.
Also, another significant part of the OS module is os.path a submodule that includes various functions for manipulating file paths. It provides the necessary tools to extract information from file paths, split them into their components, and check the validity of the paths.
Access
With os you can test whether the given path exists and learn whether it has special access rights. This functionality is implemented in the os.access(). Apart from the path, the function takes another argument, the mode value, which denotes an available way of operating or using a particular object on the system, e.g. reading or executing a file. We will cover all four possible mode values.
The mode os.F_OK is used to check the specified path for existence. If we create a new directory and want to make sure that it has been created successfully, we can simply write one line, similar to the one below:
print(os.access('some_new_project', os.F_OK)) # True
The directory some_new_project returned the Boolean True value. We specified a relative path, but you can indicate the absolute one, too.
To learn about access rights, os.access() uses uid (the identifier of a user on the system) or gid (the identifier of a user group). So, we can figure out the permissions of a certain category to directories or files. Here, the available modes are:
os.R_OKto check for readability of the path (that is, the user has the permission to see the content of the file / directory);os.W_OKto check the writability of the path (permission to write to the file / directory);os.X_OKto check if the path contents can be executed.
In the example below, we specify the path to a plaintext file in course: and the result is a Boolean value. As you can see, the file is both writable and readable, but we cannot execute it, because it is a plaintext file.
print(os.access('course/list_of_students.txt', os.R_OK)) # True
print(os.access('course/list_of_students.txt', os.W_OK)) # True
print(os.access('course/list_of_students.txt', os.X_OK)) # False
Removing directories and files
We will look at two functions for deleting directories and files.
os.remove()deletes the specified file, the relative or the full path to which we pass as an argument.os.remove('course/course_plan.txt') # checking for existence os.access('course/course_plan.txt', os.F_OK) # False
os.rmdir()removes a single specified directory. Before using it, make sure that the directory you want to delete is empty. Otherwise, anOSErrorwill be raised.os.rmdir('course/students/year') # checking for existence os.access('course/students/year', os.F_OK) # False
At this point, we will stop with the os. We will discuss the os.path functions in the two remaining sections.
Path components in os.path
os.path is mainly used for manipulating paths. Different operating systems use different conventions for pathnames, so there are actually several versions of this module with the very same functionality: for example, posixpath for UNIX-style paths or ntpath for Windows paths. We can always import os and then use os.path instead of working with them separately, the functions will work the same way as in the modules suitable for a specific operating system.
We will start with four functions that allow us to manipulate the path components.
os.path.join()joins several given components to create a new pathname. For example, we can pass it as an argument to theos.makedirs()to create nested directories. The example below shows the output for Linux first and then for Windows:print(os.path.join('more_new_projects', 'more_new_plans')) # more_new_projects/more_new_plans print(os.path.join('more_new_projects', 'more_new_plans')) # more_new_projects\\more_new_plans
os.path.split()splits a pathname into a tuple (head, tail), where tail is the very last component of the given pathname and head is everything else preceding this last component. The example below illustrates it for the UNIX-style path and then for the Windows-style one:print(os.path.split('/home/user/more_new_projects/more_new_plans')) # ('/home/user/more_new_projects', 'more_new_plans') print(os.path.split('C:\\Users\\User\\more_new_projects\\more_new_plans')) # ('C:\\Users\\User\\more_new_projects', 'more_new_plans')- Based on the latter function, the
os.path.dirname()returns the directory of the path (head).print(os.path.dirname('/home/user/more_new_projects/more_new_plans')) # /home/user/more_new_projects print(os.path.dirname('C:\\Users\\User\\more_new_projects\\more_new_plans')) # C:\\Users\\User\\more_new_projects
- Accordingly,
os.path.basename()returns the tail, whether it is the file name or the name of another directory.print(os.path.basename('/home/user/more_new_projects/more_new_plans')) # more_new_plans print(os.path.basename('C:\\Users\\User\\more_new_projects\\more_new_plans')) # more_new_plans
Path's validity
os.path also has several functions that allow us to check whether the given path is an absolute or a relative one; whether it refers to a file or directory. Below, we will list three examples of such functions.
os.path.isabs()simply checks if the path we pass to it is the absolute one; in this case it returns True:print(os.path.isabs('/home/user/more_new_projects/more_new_plans')) # True print(os.path.isabs('C:\\Users\\User\\more_new_projects\\more_new_plans')) # True print(os.path.isabs('more_new_projects\\more_new_plans')) # Falseos.path.isdir()checks whether the given path refers to a directory.print(os.path.isdir('/home/user/more_new_projects/more_new_plans')) # True
os.path.isfile(), on the contrary, checks whether the specified path refers to a file.print(os.path.isfile('/home/user/more_new_projects/more_new_plans')) # False
So far, in these two sections, we gave a small overview of the basic os.path functions.
Conclusion
In summary, the OS module in Python is a built-in module that allows programmers to interact with the operating system. In this topic, we looked at more functions of the OS module. The os.path submodule further enhances these capabilities by providing functions to manipulate and validate file paths. Understanding and utilizing the OS module and its os.path submodule can significantly simplify working with files and directories in Python. Whether you are developing a small script or a large application, the OS module is a key component in the Python programmer's toolkit.