Found 10805 Articles for Python

Python getpass Module

Arjun Thakur
Updated on 26-Jun-2020 06:31:13

932 Views

There are two functions defined in getpass module of Python’s standard library. They are useful whenever a terminal based application needs to be executed only after validating user credentials.getpass()This function prompts the user to enter a password. By default the keys entered by user in the terminal are not echoed. Also the default prompt that appears on terminal is ‘password’ which can be customized by providing a string as parameter.In following example, Python prompt is invoked from Command prompt terminal on Windows. The password entered is not echoed in the terminal.C:\python36>python Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 ... Read More

Work with ZIP archives in Python (zipfile)

George John
Updated on 25-Jun-2020 14:25:59

718 Views

The ZIP is one of the most popular file formats used for archiving and compression. It has been in use since the days of MSDOS and PC and has been used by famous PKZIP application.The zipfile module in Python’s standard library provides classes that facilitate the tools for creating, extracting, reading and writing to ZIP archives.ZipFile()This function returns a ZipFile object from a file parameter which can be a string or file object as created by built-in open() function. The function needs a mode parameter whose default value is ‘r’ although it can take ‘w’ or ‘a’ value for opening ... Read More

High-level file operations in Python (shutil)

Chandu yadav
Updated on 25-Jun-2020 14:26:52

873 Views

A number of functions for hgh level operations on files and directories have been defined in shutil module of Python’s standard library.copy()This function copies a file to a specified file in same or other directory. First parameter to the function is a string representation of existing file. Second argument is either name of resultant file or directory. If it is a directory, the file is coped in it with same name. The metadata of original file is not maintained.>>> import shutil >>> shutil.copy("hello.py", "newdir/") 'newdir/hello.py'copy2()This function is similar to copy() function except for the fact that it retains metadata of ... Read More

Unix style pathname pattern expansion in Python (glob)

Arjun Thakur
Updated on 25-Jun-2020 14:28:31

377 Views

Many a times a program needs to iterate through a list of files in the file system, often with names matching a pattern. The glob module is useful in creating lit of files in specific directory, having a certain extension, or with a certain string as a part of file name.The pattern matching mechanism used by glob module functions follows UNIX path expansion rules. This module though doesn’t expand tilde (~) and shell variables.There are mainly three function in glob moduleglob()This function returns a list of files that match the given pattern in pathname parameter. The pathname can be absolute ... Read More

Read and write tar archive files using Python (tarfile)

George John
Updated on 26-Jun-2020 07:01:45

2K+ Views

The ‘tar’ utility was originally introduced for UNIX operating system. Its purpose is to collect multiple files in a single archive file often called tarball which makes it easy to distribute the files. Functions in tarfile module of Python’s standard library help in creating tar archives and extracting from the tarball as required. The archives can be constructed with gzip, bz2 and lzma compressions or without any compression at all.Main function defined in this module is main() using which writing to tar file or reading from it is accomplished.Open()This function returns a TarFile object corresponding to file name which is ... Read More

Compression using the LZMA algorithm using Python (lzma)

Chandu yadav
Updated on 26-Jun-2020 06:16:10

3K+ Views

The Lempel–Ziv–Markov chain algorithm(LZMA) performs lossless data compression using a dictionary compression scheme featuring a higher compression ratio than other compression algorithms. Python’s lzma module consists of classes and convenience functions for compression and decompression of data with LZMA algorithm.Although the functionality in this module is similar to that of bz2 module, the LZMAFile class is not thread safe as compared to BZ2File class.Here again, open() function in lzma module is a very easiest way to open lzma-compressed file object.open()This function opens a LZMA-compressed file and returns a file object. The function requires two main parameters – file name and ... Read More

Python Support for bzip2 compression (bz2)

Ankith Reddy
Updated on 26-Jun-2020 06:20:36

2K+ Views

The bzip2 is an open source algorithm for compression and decompression of files. Python’s bz2 module provides functionality to implement bzip2 algorithm programmatically.The open() function is the primary interface to this module.Open()This function opens a bzip2 compressed file and returns a file object. The file can be opened as binary/text mode with read/write permission. The function performs compression based on compressionlevel argument between 1 to 9.write()When the file is opened in ‘w’ or ‘wb’ mode, this function is available to the file object. In binary mode, it writes compressed binary data to the file. In normal text mode, the file ... Read More

Python Support for gzip files (gzip)

Arjun Thakur
Updated on 25-Jun-2020 13:58:33

11K+ Views

GZip application is used for compression and decompression of files. It is a part of GNU project. Python’s gzip module is the interface to GZip application. The gzip data compression algorithm itself is based on zlib module.The gzip module contains definition of GzipFile class along with its methods. It also caontains convenience function open(), compress() and decompress().Easiest way to achieve compression and decompression is by using above mentioned functions.open()This function opens a gzip-compressed file in binary or text mode and returns a file like object, which may be physical file, a string or byte object. By default, the file is ... Read More

Compression compatible with gzip in Python (zlib)

George John
Updated on 25-Jun-2020 14:02:35

637 Views

The zlib module provides Python’s implementation of Zlib compression library (http://www.zlib.net) which is a part of GNU project.This article discusses important functions defined in zlib module.compress()This function is the primary interface to this module along with decompress() function. This function returns byte object by compressing the data given to it as parameter. The function has another parameter called level which controls the extent of compression. It an integer between 0 to 9. Lowest value 0 stands for no compression and 9 stands for best compression. Higher the level of compression, greater the length of compressed byte object.decompress()This function does the ... Read More

JSON encoder and decoder package in Python

Chandu yadav
Updated on 26-Jun-2020 06:42:28

2K+ Views

JSON stands for JavaScript Object Notation. It is a lightweight data interchange format. It is similar to pickle. However, pickle serialization is Python specific whereas JSON format is implemented by many languages. The json module in Python’s standard library implements object serialization functionality that is similar to pickle and marshal modules.Just as in pickle module, the json module also provides dumps() and loads() function for serialization of Python object into JSON encoded string, and dump() and load() functions write and read serialized Python objects to/from file.dumps()This function converts the object into JSON format.loads()This function converts a JSON string back to ... Read More

Advertisements