Python OS module allows us to use the operating system dependent functionalities and to interact with the underlying operating system in several different ways. For example, we can work with files, change the environment variables, and we can move files around, etc. This is as same as overriding all the os built-in functionalities in a module and using them in a file I/O and system handling.
Table of Contents
Python import os
Now, as it is a built-in module, so we don’t have to install any third-party libraries. We can import the os module in our program like this:
Copyimport os # importing the complete os module from os import name # importing a variable from the os module
Let’s look at a simple example of using the os module.
Copyimport os print(dir(os))
NOTE: By printing this built-in dir() function and pass the os module, it shows us all of the attributes and methods that we have access to within this module.
OS Module Common Functions
OS module provides some callable methods and some variables. Some of the common methods for different functional categories are:
- Manipulating directories:
- Removing a file:
- Renaming files/directories:
- Using more than one process:
- User id and process id:
- getgid(), os.getuid(), os.getpid()
- More about directories and files:
- Cross-plateform os attributes:
- Accessing environment variables:
Common Functions Explanation and Usage
- os.name: This is the name of the imported operating system dependent module. Some of the registered module are – ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’ and ‘riscos’.
- os.error: It is environment error class for I/O errors and OSError and is raised when any function returns any system-related error. Each of the or module functions returns these errors when any invalid or inaccessible file is triggered in the line-of-code.
Copyimport os try: filename = 'abcd2.txt' f = open(filename, 'r') text = f.read() f.close() except os.error: print('Problem reading: ' + filename)
- os.system(): executing a shell command.
Copycmd = "git --version" returned_value = os.system(cmd) # returns the exit code in unix print('returned value:', returned_value)
- os.environ(): It is known as the value of an object which returns all the directories of all user environment variables .E.g. ‘HOME’-directory environment variables
Copyimport os os.chdir('C:/Users/user/Desktop/temp') # returns all the environment variables print(os.environ) # to get in particular print(os.environ.get('TEMP'))
- os.getcwd(): returns the current working directory(CWD) in which the user is currently.
- os.chdir(): to change the directory.
- os.listdir(): It returns a list of files and the folders in the current directory.
['.git', '1802.04103.pdf', '1st year', '2K16-CO-200', 'abc.txt', 'afcat',]
- os.popen(command[, mode[, bufsize]]) : it opens a pipe to or from command. It returns an open file object connected to the pipe, which can be read or written depending on whether the mode is ‘r’ (default) or ‘w’.
Copyimport os fd = "abc.txt" # popen() is similar to open() file = open(fd, 'w') file.write("Hello") file.close() file = open(fd, 'r') text = file.read() print(text) # popen()and accesses the file directly file = os.popen(fd, 'w') file.write("Hello")
- os.close(): Close file descriptor fd.
Note: It must be applied to a file descriptor returned by os.open() or pipe() built-in functions of os module.
Copyfd = "abc.txt" file = open(fd, 'r') text = file.read() print(text) os.close(file)
NOTE: Error is thrown for the non-existence of the file or the permission privileges.
- os.getgid(), os.getuid(), os.getpid(), and os.stat(): getgid() returns the real group id for the current process. The getuid() function returns the current process’s user id and getpid() returns real process id of the current process. The os.stat() function returns the list of details about the file or the directory name given in the argument.
Copyprint(os.stat('abcd1')) # for some simplified and particular details # we can use dot operator and that attribute name # this returns the timestamp of last modification time print(os.stat('abcd1').st_mtime) # this returns the size of the file in bytes print(os.stat('abcd1').st_size)
- os.walk(): it is a generator that yields a couple of three values as it is walking the directory tree and for each directory that it traverses and produces the directory path, the direct within that path and the files within that path. It is useful to keep track of all the directories.
Copyos.chdir('C:/Users/user/Desktop/temp') # returns a 3-tuple for dirpath, dirname, filename in os.walk('C:/Users/user/Desktop/temp'): print('Current path: ',dirpath) print('Directories: ', dirname) print('Files: ', filename) print()
- os.mkdir() and os.makedirs(): To create new directories.
Differences: makedirs() creates all the intermediate directories if they don’t exist already and mkdir() can create a single sub-directory and will throw an exception if intermediate directories that don’t exist are specified.
Copy['.git', '1802.04103.pdf', '1st year', '2K16-CO-200', 'abc.txt', 'abcd', 'abcd1', 'afcat',] Traceback (most recent call last): File "C:/Users/user/.PyCharmCE2017.3/config/scratches/scratch.py", line 18, in <module> os.mkdir('abcd2/subdir') FileNotFoundError: [WinError 3] The system cannot find the path specified: 'abcd2/subdir' ['.git', '1802.04103.pdf', '1st year', '2K16-CO-200', 'abc.txt', 'abcd', 'abcd1', 'abcd2', 'afcat'] Process finished with exit code 1
NOTE: The line 18 code thrown an error for creating a directory along with a subdirectory because os.mkdir() don’t work in a tree fashion.
- os.rmdir() and os.removedirs(): same as os.mkdir() and os.makedirs() os.rmdir() will not remove the intermediate directory where as os.removedirs() will remove the intermediate directories. Observe the below code and output as continued with previous commands and directories.
Copyos.rmdir('abcd') os.removedirs('abcd2/subdir') print(os.listdir())
OUTPUT: [‘.git’, ‘1802.04103.pdf’, ‘1st year’, ‘2K16-CO-200’, ‘abc.txt’, ‘abcd1’, , ‘afcat’]
- os.rename():to rename a file or a folder.In arguments pass the original file name first and then the new name of the file.
Copyos.chdir('C:/Users/user/Desktop/temp') print(os.listdir()) os.rename('xyz.txt','abc.txt') print(os.listdir())
- os.remove(): It removes the path of a file. It takes path string as a variable.
Copyimport os os.chdir('C:/Users/user/Desktop/temp') print(os.listdir()) os.chdir('C:/Users/user/Desktop/') os.remove('temp/abc.txt') os.chdir('C:/Users/user/Desktop/temp') print(os.listdir())
Advantages of Using Python OS Module
- This module is useful if you want to make your programs platform-independent, i.e. with the use of python os module you can make your code run smoothly on linux as well as on windows without requiring any changes to be made.
- It represents the generic system functionality.
At this point will be familiar with the python os module. In this article, we learned the following:
- how to work with environment variables
- change directories and discover your current working directory
- create and remove folders and files
- rename files / folders
- start a file with its associated application
- walk a directory
- work with paths
There are a lot of other functions in the os module that has not been discussed here because they are not commonly used. Be sure to read the documentation https://docs.python.org/3/library/os.html to see what else you can do with the python os module