Tutorialspoint

How to extract all the .txt files from a zip file using Python?

How do I extract all the .txt files from a zip file in Python?
Rajendra Dharmkar
Answered on 28th Dec, 2017

In order to extract all .txt files from a zip, you'll need to loop over all files in the zipfile, check if a file is txt file or not. If it is a txt file then extract it. For this we'll use the zipfile module and its extract function. For ... Read More

How are files extracted from a tar file using Python?

How do I extract files from a tar file in Python?
Rajendra Dharmkar
Answered on 28th Dec, 2017

You can use the tarfile module to read and write tar files. To extract a tar file, you need to first open the file and then use the extract method of the tarfile module. For example, import tarfile my_tar = tarfile.open('my_tar.tar.gz') my_tar.extractall('./my_folder') # specify which folder to extract to my_tar.close()This ... Read More

How are files added to a zip file using Python?

How do I add files to a zip file in Python?
Rajendra Dharmkar
Answered on 28th Dec, 2017

Use the zipfile module to create a zip archive of a directory. Walk the directory tree using os.walk and add all the files in it recursively. For example, import os import zipfile def zipdir(path, ziph):     # ziph is zipfile handle     for root, dirs, files in os.walk(path): ... Read More

How are files added to a tar file using Python?

How do I add files to a tar file in Python?
Rajendra Dharmkar
Answered on 28th Dec, 2017

Use the tarfile module to create a zip archive of a directory. Walk the directory tree using os.walk and add all the files in it recursively. For example, import os import tarfile def tardir(path, tar_name):     with tarfile.open(tar_name, "w:gz") as tar_handle:         for root, dirs, files ... Read More

How to create a zip file using Python?

How do I create a zip file in Python?
Rajendra Dharmkar
Answered on 28th Dec, 2017

Use the zipfile module to create a zip archive of a directory. Walk the directory tree using os.walk and add all the files in it recursively. For example, import os import zipfile def zipdir(path, ziph):     # ziph is zipfile handle     for root, dirs, files in os.walk(path): ... Read More

How to create a tar file using Python?

How do I create a compressed tar file in Python?
Rajendra Dharmkar
Answered on 28th Dec, 2017

Use the tarfile module to create a zip archive of a directory. Walk the directory tree using os.walk and add all the files in it recursively. For example, import os import tarfile def tardir(path, tar_name):     with tarfile.open(tar_name, "w:gz") as tar_handle:         for root, dirs, files ... Read More

How are entire non-empty directory trees removed using Python?

How do I remove entire non-empty  directory trees in Python?
Rajendra Dharmkar
Answered on 28th Dec, 2017

If you want to delete a folder containing all files you want to remove, you can remove the folder as follows:>>> import shutil >>> shutil.rmtree('my_folder')If you want to remove an empty folder, you can call the rmdir function in os module. For example, >>> import os >>> os.rmdir('my_folder') Read More

How is a file read using a limited buffer size using Python?

How do I read a file using a limited buffer size in Python?
Rajendra Dharmkar
Answered on 28th Dec, 2017

You can read a file using a limited buffer by specifying the buffer size in the read function. It takes the number of bytes you want to read from the current position of the pointer in the file. For example, with open('my_file.txt', 'r') as f:     print(f.read(10))  # Read ... Read More

How an entire file is read into buffer and returned as a string in Python?

How do I read an entire file into buffer and return it as a string in Python?
Rajendra Dharmkar
Answered on 28th Dec, 2017

Python's File class' read function automatically manages to do that. When you open a file in python and call the read function on the file handle, it will read the whole file in a string and return that string. For example, with open('my_file.txt', 'r') as f:     file_content = ... Read More

How to print all the keys of a dictionary in Python?

I have a dictionary object. I want only the keys of dictionary. How to print all the keys of a dictionary in Python?
Pythonic
Answered on 28th Dec, 2017

Dictionary object possesses keys() method which does this job for us. >>> D1 = {1:'a', 2:'b', 3:'c'} >>> D1.keys() dict_keys([1, 2, 3]) >>> list(D1.keys()) [1, 2, 3] iterable list object can be traversed using for loop >>> L1 = list(D1.keys()) >>> for i in ... Read More

How to print all the values of a dictionary in Python?

I have a dictionary object. I want only the values from it. How to print all the values of a dictionary in Python?
Pythonic
Answered on 28th Dec, 2017

Dictionary object possesses values() method which does this job for us. >>> D1 = {1:'a', 2:'b', 3:'c'} >>> D1.values() dict_values(['a', 'b', 'c']) >>> list(D1.values()) ['a', 'b', 'c'] You can also get corresponding value by iterating through lidt of keys returned by keys() method of dictionary >>> L1 ... Read More

How to iterate through a dictionary in Python?

Is Python dictionary an iterable object? How to iterate through a dictionary in Python? Can a for loop be used? Explain with suitable example
Pythonic
Answered on 28th Dec, 2017

There are two ways of iterating through a Python dictionary object. One is to fetch associated value for each key in keys() list. >>> D1 = {1:'a', 2:'b', 3:'c'} >>> for k in D1.keys(): print (k, D1[k]) 1 a 2 b 3 c There ... Read More

How to sort a dictionary in Python by keys?

Dictionary elements do not have index. In that case How to sort a dictionary in Python by keys? Give a suitable example
Pythonic
Answered on 28th Dec, 2017

Standard distribution of Python contains collections module. It has definitions of high performance container data types. OrderedDict is a sub class of dictionary which remembers the order of entries added in dictionary object. When iterating over an ordered dictionary, the items are returned in the order their keys were first ... Read More

How to sort a dictionary in Python by values?

Dictionary elements do not have index. In that case How to sort a dictionary in Python by values? Give a suitable example
Pythonic
Answered on 28th Dec, 2017

Standard distribution of Python contains collections module. It has definitions of high performance container data types. OrderedDict is a sub class of dictionary which remembers the order of entries added in dictionary object. When iterating over an ordered dictionary, the items are returned in the order their keys were first ... Read More

What is the maximum value of float in Python?

What is the maximum value of float in Python? Which function should be used? Is the value different for different operating system? 
Pythonic
Answered on 28th Dec, 2017

In sys module, a struct sequence (tuple of named elements) called float_info has been defined. In this structure, an element max returns maximum representable finite float number. >>> import sys >>> sys.float_info.max 1.7976931348623157e+308

Advertisements
Loading...
Unanswered Questions View All

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.