Categories
Python Answers

How to unzip files with Python?

Sometimes, we want to unzip files with Python.

In this article, we’ll look at how to unzip files with Python.

How to unzip files with Python?

To unzip files with Python, we can use the zipfile module.

For instance, we write:

import zipfile

path_to_zip_file = 'test.zip'
directory_to_extract_to = './test'

with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
    zip_ref.extractall(directory_to_extract_to)

We read the zip file into a Python object with zipfile.ZipFile(path_to_zip_file, 'r').

Then we call extractall on the returned zip file object to do the extraction.

We specify the directory_to_extract_to to extract the zip file’s contents.

Now we should see the test folder having the content of the zip file after the code is run.

Conclusion

To unzip files with Python, we can use the zipfile module.

Categories
Python Answers

How to get the number of elements in a list with Python?

Sometimes, we want to get the number of elements in a list with Python.

In this article, we’ll look at how to get the number of elements in a list with Python.

How to get the number of elements in a list with Python?

To get the number of elements in a list with Python, we can use the len function.

For instance, we write:

l = len([1, 2, 3])
print(l)

We call len with a list as its argument to return the number of items in the list and assign that to l.

Therefore, l is 3.

Conclusion

To get the number of elements in a list with Python, we can use the len function.

Categories
Python Answers

How to print all instances of a class with Python?

Sometimes, we want to print all instances of a class with Python.

In this article, we’ll look at how to print all instances of a class with Python.

How to print all instances of a class with Python?

To print all instances of a class with Python, we can use the gc module.

For instance, we write:

import gc


class A:
    pass


a1 = A()
a2 = A()
for obj in gc.get_objects():
    if isinstance(obj, A):
        print(obj)

We have the A class and we create 2 instances of it, which we assigned to a1 and a2.

Then we loop through the objects in memory with gc.get_objects with a for loop.

And we check if each obj is an instance of A with isinstance.

If it is, we print it.

Therefore, we see:

<__main__.A object at 0x7f36601f5b80>
<__main__.A object at 0x7f36601f57c0>

from the print output.

Conclusion

To print all instances of a class with Python, we can use the gc module.

Categories
Python Answers

How to get a union of strings with Python Pandas groupby?

Sometimes, we want to get a union of strings with Python Pandas groupby.

In this article, we’ll look at how to get a union of strings with Python Pandas groupby.

How to get a union of strings with Python Pandas groupby?

To get a union of strings with Python Pandas groupby, we can use groupby with apply.

For instance, we write:

import pandas as pd

df = pd.DataFrame({'A': [1, 1, 3], 'B': [4, 5, 6]})
s = df.groupby('A')['B'].apply(list)
print(s)

to create the df data frame with pd.DataFrame.

Then we call df.groupby with 'A' and 'B' to group the values in column 'B' by the values of column 'A'.

And then we call apply with list to put the grouped values into lists.

Therefore, s is:

A
1    [4, 5]
3       [6]
Name: B, dtype: object

Conclusion

To get a union of strings with Python Pandas groupby, we can use groupby with apply.

Categories
Python Answers

How to find a file with Python?

Sometimes, we want to find a file with Python.

In this article, we’ll look at how to find a file with Python.

How to find a file with Python?

To find a file with Python, we can use the os.walk method.

For instance, we write:

import os


def find(name, path):
    for root, dirs, files in os.walk(path):
        if name in files:
            return os.path.join(root, name)


print(find('data.xlsx', './'))

We defined the find function that calls os.walk with path.

In the function we loop through the files and directories returned from the iterator starting from the path‘s level and below.

And we check if the name is in the files array to check if the file with the given name is in the files array.

If that’s True, then we return the full path of the file with os.path.join.

Therefore, if the file is found, then we get something like './data.xlsx'. from print.

Conclusion

To find a file with Python, we can use the os.walk method.