Found 34488 Articles for Programming

High-level file operations in Python (shutil)

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

975 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

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

Deque in Java

Samual Sam
Updated on 26-Jun-2020 06:18:01

660 Views

The dequeue is a double ended queue and data elements can be added or removed from either end. The dequeue in Java is implemented using the java.util.Deque interface which is a subtype of the java.util.Queue interface.A program that demonstrates some of the methods of a dequeue is given as follows −Example Live Demoimport java.util.*; public class Example {    public static void main(String[] args) {       Deque d = new LinkedList();       d.add("5");       d.addFirst("1");       d.addLast("9");       d.push("7");       d.offer("8");       d.offerFirst("6");       d.offerLast("2"); ... 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

Time Functions in Java

karthikeya Boyini
Updated on 26-Jun-2020 06:19:02

685 Views

Java provides the Date class available in java.util package, this class encapsulates the current date and time. The time functions can be accessed from the java.util.Date class. This represents an instance of time with millisecond precision.One of the time function in Java is the getTime() function. It returns the number of milliseconds that have passed since January 1, 1970, 00:00:00 GMT. A program that demonstrates this is given as follows −Example Live Demoimport java.util.*; public class Example {    public static void main(String[] args) {       Date d = new Date(95, 7, 15);       long num = ... 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

Multiplication of two Matrices using Java

Samual Sam
Updated on 26-Jun-2020 06:22:34

4K+ Views

Matrix multiplication leads to a new matrix by multiplying 2 matrices. But this is only possible if the columns of the first matrix are equal to the rows of the second matrix. An example of matrix multiplication with square matrices is given as follows.Example Live Demopublic class Example {    public static void main(String args[]) {       int n = 3;       int[][] a = { {5, 2, 3}, {2, 6, 3}, {6, 9, 1} };       int[][] b = { {2, 7, 5}, {1, 4, 3}, {1, 2, 1} };       int[][] ... 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

Advertisements