Python has numerous libraries like os
, sys
, json
, yaml
etc that a DevOps Engineer uses in day to day tasks.
How "import" works in Python
Importing is the process of loading code from a Python module into the current script.
This allows us to use the functions and variables defined in the module in our current script, as well as any additional modules that the imported module may depend on.
Once a module is imported, we can use any of the functions and variables defined in the module by using the "dot" notation.
from keyword: using the "from" keyword, we can import specific functions and variables from a module.
as keyword: used to create short name for modules.
dir keyword: used to view the name of all the functions and variables defined in a module.
OS Module
A built-in library that provides functions for interacting with the os.
It allows us to perform a wide varieties of tasks, such as reading a file, writing to a file, interacting with the file system, and running system commands.
os.getcwd(): To get the location of the current working directory.
-
os.chdir(): To change the current working directory. This method changes the CWD to a specified path. It only takes a single argument as a new directory path.
Note: The current working directory is the folder in which the Python script is operating. -
os.mkdir(): used to create a directory named path with the specified numeric mode. This method raises "FileExistsError" if the directory to be created already exists.
os.makedirs(): 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.
os.listdir(): 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.
os.remove(): 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.
os.rmdir(): used to remove or delete an empty directory. OSError will be raised if the specified path is not an empty directory.
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.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.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.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’.
SYS Module
The python sys module provides functions and variables which are used to manipulate different parts of the Python Runtime Environment. It lets us access system-specific parameters and functions.
sys.version: returns a string containing the version of Python Interpreter with some additional information.
sys.path is a built-in variable within the sys module that returns the list of directories that the interpreter will search for the required module.
sys.argv: returns a list of command line arguments passed to a Python script. The item at index 0 in this list is always the name of the script. The rest of the arguments are stored at the subsequent indices.
FILE I/O
Modes in file:
read(r): This mode opens the file for reading only and gives an error if the file doesn't exist. This is the default mode if no mode is passed as a parameter.
write(w): This mode opens the file for writing only and creates a new file if the file doesn't exist.
append(a): This mode opens the file for appending to the lines only and creates a new file if the file doesn't exist.
create(x): This mode creates a file and gives an error if the file already exist.
text(t): This mode is used to handle text files. There is no difference between r & rt or w & wt, since text mode is default.
binary(b): This mode is used to handle binary files(images, pdfs, etc.).
By with open, you do not need to close the file, it will close automatically/.
readlines(): reads a single line from the file. If we want to read multiple lines, we can use a loop.
writelines(): writes a sequence of strings to a file. The sequence can be any iterable object, such as a list or a tuple.
seek(): allows to move the current position within a file to a specific point. The position is specified in bytes and we can move either forward or backward from the current position.
Converting YAML to JSON
Give output on the screen:
Give output to a file:
Conclusion
In conclusion, the os
module in Python provides a way to interact with the operating system, offering functionalities such as file and directory manipulation, process management, and more.
The sys
module, on the other hand, provides access to some variables used or maintained by the Python interpreter and functions that interact with the interpreter.
Regarding file input/output (I/O) operations, Python supports different modes for opening files using the built-in open()
function. Common modes include:
Read Mode (
'r'
): Used for reading the contents of a file.Write Mode (
'w'
): Used for writing to a file, creating a new file or truncating an existing one.Append Mode (
'a'
): Used for appending data to the end of a file.Binary Mode (
'b'
): Used for handling binary data in addition to text data.Read and Write Mode (
'r+'
or'w+'
): Used for both reading and writing to a file.
The yaml
module allows for parsing YAML-formatted data, while the json
module enables the generation of equivalent JSON output. By using these modules in tandem, developers can seamlessly translate data between the YAML and JSON formats, ensuring compatibility and flexibility in data interchange within Python applications.
*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊
~Smriti Sharma✌