Found 10784 Articles for Python

Unix style pathname pattern expansion in Python (glob)

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

416 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

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

700 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

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

Advertisements