Author - StudySection Post Views - 426 views
python

Basic Modules In Python

Introduction

A module is a file that contains a set of functions that you can include in your application. It can be imported into other modules or into the main module. We can consider a module as a code Library. A module also allows you to organize your code logically.

Python Modules

Here we will discuss some basic build-in modules that are generally used in python:

  • sys
  • os
  • json
  • platform
  • csv
  • request
  1. sys-(module)

    The python sys module provides functions and variables that are used to manipulate different parts of the Python Runtime Environment. It lets us access system-specific parameters and functions.

    Import this module using the ‘import sys’ statement.

    • sys.argv: It returns a list that contains the command line arguments passed to python script.
    • sys.modules: This function provides existing python modules that have been imported.
    • sys.path: This function shows the PYTHON PATH set in the current system. It is an environment variable that is a search path for all the python modules.
    • sys.exit: This function is used to exit from either the Python console or command prompt, and also used to exit from the program in case of an exception.
    • sys.platform: This function is used to identify the platform on which we are working.
  2. os-(module)

    It provides various functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module allows a portable method of using operating system dependent functionality. The *os* and *os.path* modules include many functions to interact with the system.

    • os.name: It checks whether certain os specific modules are available or not. It gives the name of the operating system dependent module that are imported.e.g: ‘posix’, ‘nt’, ‘os2’, ‘ce’.
    • os.getcwd: This function returns the current working directory(CWD) of the file used to execute the code, it can vary from system to system.
    • os.listdir(‘.’): It is used to print files and directories in the current directory.
    • os.error: All the functions within this module raise OSError in case of invalid or inaccessible file names and paths, or other arguments that have the right type but aren’t accepted by the operating system. os.error is an alias for built-in OSError exceptions.
  3. json-(Module)

    It could be the syntax for saving and exchanging data. JSON is text, written with JavaScript object notation.
    Python contains a built-in package called JSON, which might be used to work with JSON data.
    Import this module using the ‘import JSON’ statement.

    -Parse json – If you have a json file then import it into python.

    If you’ve got a json string you’ll be able to parse it by using the json.loads() method.


    x = ‘{“name”:”testUser”,”age”:22,”city”:”testCity”}’
    y=json.loads(x)
    print(y[“age”])

    -If you get a Python object, you can convert it into a JSON string by using the json.dumps() method.

    x={“name”:”testUser”,”age”:22,”city”:”testCity”}
    y=json.dumps(x)
    print(y)

  4. Platform

    Python defines a built-in module platform that provides system information. The Platform module is used to retrieve as much possible information about the platform on which the program is being currently executed. It gives information about the device, it’s OS, node, OS version and Python version, etc.

    For displaying platform processor:    ‘print(platform.processor())’

    By using this statement you can display the platform module information.

    Platform architecture:    ‘print(platform.architecture())’

    This function returns a tuple that stores information about the bit architecture(number of bits in the platform processor) and linkage format( defines how names can or can not refer to the same entity throughout the entire program or one single translation unit).

    Displaying machine type:    ‘print(platform.machine())’

    This gives information about the width or size of registers available in the core.

    platform node:    ‘print(platform.node())’

    This function returns a string that displays the information about the node basically the system’s network name.

    platform.platform():     ‘print(platform.platform())’

    This function returns a single string containing as much useful information that is to retrieve the system. The output may differ on different systems.

  5. CSV Module

    The CSV module is one of the modules in Python that provides classes for reading and writing tabular information in CSV file format.

    Reading the CSV file format in python


    import csv
    with open(‘Giants.csv’, mode =’r’)as file:
    csvFile = csv.reader(file)
    for lines in csvFile:
    print(lines)

  6. Request Module

    This module allows you to send HTTP requests by using Python. The HTTP request returns a Response Object with all the response data that contains content, encoding, status, etc.

    You can install the request module by the following command:

    “Pip install requests”

    You can make a request to a web page, and print the response text:

    Import request
    x = requests.get(“request_url”)
    print(x.text)

People having good command over the French language can get a French certification from StudySection. StudySection offers both beginner level and expert level French certification exams to test the ability to communicate in the French language.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri