Found 10784 Articles for Python

Random access to text lines in Python (linecache)

Chandu yadav
Updated on 25-Jun-2020 13:36:20

784 Views

Purpose of linecache module in Python’s standard library is to facilitate random access to any text file, although this module is extensively used by Python’s traceback module to generate error trace stack. Further prettyprints of reading are held in a cache so that it saves time while reading lines repeatedly.The most important function in this module is getline() which reads a specified line number from given file. Following is the list of functions −getline(file, x)This function returns xth line from file. If not present it will return empty string. If the file is not present in current path, function ties ... Read More

Iterate over lines from multiple input streams in Python

Ankith Reddy
Updated on 25-Jun-2020 13:43:08

515 Views

Python’s built-in open() function opens one file in read/write mode and read/write operations on it. To perform processing on multiple files in a batch, one has to use fileinput module of Python’s standard library. This module provides a Fileinput class with functionality of iterating over files. The module also defines helper functions for the same purpose.Primary interface to this module is input() function. This function returns instance of Fileinput class.fileinput.input(files, inplace, mode)The files parameter is name of one or more files to be read one by one. Each file acts as a generator and using a for loop it can ... Read More

Object-oriented filesystem paths in Python (pathlib)

Arjun Thakur
Updated on 25-Jun-2020 13:47:38

695 Views

The pathlib module provides an object oriented approach to handling filesystem paths. The module also provides functionality appropriate for various operating systems. Classes defined in this module are of two types – pure path types and concrete path types. While pure paths can only perform purely computational operations, concrete paths are capable of doing I/O operations too.pathlib module defines following classes −Sr.No.Module & Description1PurePathThe base class for all other classes2Pathsubclassed from PurePath. This is a concrete class that represents filesystem path.3PosixPathPath subclass for non-Windows OS4WindowsPathPath subclass for Windows systems5PurePosixPathPurePath subclass for non-Windows systems6PureWindowsPathPurePath subclass for Windows systemsWhen instance of Path ... Read More

Python object persistence (shelve)

George John
Updated on 25-Jun-2020 13:48:57

4K+ Views

The shelve module in Python’s standard library is a simple yet effective tool for persistent data storage when using a relational database solution is not required. The shelf object defined in this module is dictionary-like object which is persistently stored in a disk file. This creates afile similar to dbm database on UNIX like systems. Only string data type can be used as key in this special dictionary object, whereas any picklable object can serve as value.The shelve module defines three classes as follows −Sr.No.Module & Description1ShelfThis is the base class for shelf implementations. It is initialized with dict-like object.2BsdDbShelf This ... Read More

How to install Python MySQLdb module using pip?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

3K+ Views

To install python MySQLdb module, we need to install Python current version i.e. 3.7 We need to find the location of Python Scripts where pip command is located. First, open the cmd and reach the location of Python Scripts. To open cmd, press “Windows+R” and type cmd. Here is the snapshot − Now reach where scripts are located. We will now install “MySQLdb” module. The steps are displayed in the following screenshot.

Keywords in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

3K+ Views

Like other languages, Python also has some reserved words. These words hold some special meaning. Sometimes it may be a command, or a parameter etc. We cannot use keywords as variable names. The Python Keywords are True False class def return if elif else try except raise finally for in is not from import global lambda nonlocal pass while break continue and with as yield del or assert None The True & False Keywords The True and False are the truth ... Read More

Precision Handling in Python

Samual Sam
Updated on 30-Jul-2019 22:30:23

3K+ Views

Python can handle the precision of floating point numbers using different functions. Most functions for precision handling are defined in the math module. So to use them, at first we have to import the math module, into the current namespace. import math Now we will see some of the functions for precision handling. The trunc() function The trunc() method is used to remove all fractional part from a floating point number. So it returns only the integer part from the number. The ceil() function The ceil() method is used to return the Ceiling value of a ... Read More

Formatted text in Linux Terminal using Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

225 Views

In this section, we will see how to print formatted texts in Linux terminal. By formatting, we can change the text color, style, and some special features. Linux terminal supports some ANSI escape sequences to control the formatting, color and other features. So we have to embed some bytes with the text. So when the terminal is trying to interpret them, those formatting will be effective. The general syntax of ANSI escape sequence is like below − \x1b[A;B;C A is the Text Formatting Style B is the Text Color or Foreground Color C is the Background ... Read More

Reading and Writing to text files in Python

Samual Sam
Updated on 30-Jul-2019 22:30:23

1K+ Views

Like other languages, Python provides some inbuilt functions for reading, writing, or accessing files. Python can handle mainly two types of files. The normal text file and the binary files. For the text files, each lines are terminated with a special character '' (It is known as EOL or End Of Line). For the Binary file, there is no line ending character. It saves the data after converting the content into bit stream. In this section we will discuss about the text files. File Accessing Modes Sr.No Modes & Description 1 r It is Read ... Read More

Generate a graph using Dictionary in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

2K+ Views

The graphs can be implemented using Dictionary in Python. In the dictionary, each key will be the vertices, and as value, it holds a list of connected vertices. So the entire structure will look like Adjacency list of a graph G(V, E). We can use the basic dictionary object, but we are using default dict. It has some additional features. It has one additional writable instance variable. We are providing a text file, which contains the number of vertices, number of edges, names of vertices, and the list of edges. For undirected graph, we are providing two edges like ... Read More

Advertisements