Found 10805 Articles for Python

Reading and Writing CSV File using Python

Ankith Reddy
Updated on 25-Jun-2020 14:14:39

2K+ Views

CSV (stands for comma separated values) format is a commonly used data format used by spreadsheets. The csv module in Python’s standard library presents classes and methods to perform read/write operations on CSV files.writer()This function in csv module returns a writer object that converts data into a delimited string and stores in a file object. The function needs a file object with write permission as a parameter. Every row written in the file issues a newline character. To prevent additional space between lines, newline parameter is set to ‘’.The writer class has following methodswriterow()This function writes items in an iterable ... Read More

Generate temporary files and directories using Python

Arjun Thakur
Updated on 25-Jun-2020 14:21:25

14K+ Views

The tempfile module in standard library defines functions for creating temporary files and directories. They are created in special temp directories that are defined by operating system file systems. For example, under Windows the temp folder resides in profile/AppData/Local/Temp while in linux the temporary files are held in /tmp directory.Following functions are defined in tempfile moduleTemporaryFile()This function creates a temporary file in the temp directory and returns a file object, similar to built-in open() function. The file is opened in wb+ mode by default, which means it can be simultaneously used to read/write binary data in it. What is important, ... Read More

File and Directory Comparisons in Python

George John
Updated on 25-Jun-2020 13:34:45

5K+ Views

Python’s standard library has filecmp module that defines functions for comparison of files and directories. This comparison takes into consideration the properties of files in addition to data in them.Example codes in this article use following file and directory structure.Two directories dir1 and dir2 are first created under current working directory. They contain following files.--dir1/newfile.txt-- This is a file in dir1 --dir1/file1.txt-- Hello Python --dir1/file2.txt-- Python Standard Library --dir2/file1.txt-- Hello Python --dir2/file2.txt-- Python LibraryLet us now describe various comparison functions in filecmp module.filecmp.cmp(f1, f2, shallow=True)This function compares the two files and returns True if they are identical, False otherwise. The ... Read More

Random access to text lines in Python (linecache)

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

699 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

470 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

643 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

2K+ 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

2K+ 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

Advertisements