OS Interaction with OS Library
The OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependent functionality. The os and os.path modules include many functions to interact with the file system.
Handling the Current Working Directory
Consider Current Working Directory(CWD) as a folder, where the Python is operating. Whenever the files are called only by their name, Python assumes that it starts in the CWD which means that name-only reference will be successful only if the file is in the Python’s CWD.
The folder where the Python script is running is known as the Current Directory. This is not the path where the Python script is located.
If you want to find the directory where the script is located, use os.path.realpath(__file__)
. It will return a string containing the absolute path to the running script.
Creating a Directory
There are different methods available in the OS module for creating a directory. These are –
os.mkdir()
os.makedirs()
Using os.mkdir()
os.mkdir()
method in Python is used to create a directory named path with the specified numeric mode. This method raises FileExistsError if the directory to be created already exists.
we can pass mode parameter to mkdir
function
Using os.makedirs()
os.makedirs()
method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os.makedirs()
method will create them all.
Listing out Files and Directories with Python
os.listdir()
method in Python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then the list of files and directories in the current working directory will be returned.
Deleting Directory or Files using Python
OS module proves different methods for removing directories and files in Python. These are –
Using os.remove()
os.remove()
method in Python is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.
Using os.rmdir()
os.rmdir()
method in Python is used to remove or delete an empty directory. OSError will be raised if the specified path is not an empty directory.
Commonly Used Functions
os.getpid method in Python is used to get the process ID of the current process.
os.name This function gives the name of the operating system dependent module imported. The following names have currently been registered: ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’ and ‘riscos’.
os.error All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. os.error is an alias for built-in OSError exception.
os.popen This method opens a pipe to or from command. The return value can be read or written depending on whether the mode is ‘r’ or ‘w’.
os.close Close file descriptor fd. A file opened using open(), can be closed by close()only. But file opened through os.popen(), can be closed with close() or os.close(). If we try closing a file opened with open(), using os.close(), Python would throw TypeError.
os.rename A file old.txt can be renamed to new.txt, using the function os.rename(). The name of the file changes only if, the file exists and the user has sufficient privilege permission to change the file.
os.path.exists This method will check whether a file exists or not by passing the name of the file as a parameter. OS module has a sub-module named PATH by using which we can perform many more functions.
os.path.getsize In this method, python will give us the size of the file in bytes. To use this method we need to pass the name of the file as a parameter.
Last updated