Categories
Python Answers

How to check type of files without extensions with Python?

Sometimes, we want to check type of files without extensions with Python.

In this article, we’ll look at how to check type of files without extensions with Python.

How to check type of files without extensions with Python?

To check type of files without extensions with Python, we can use the magic library.

To install it, we run

pip install python-magic

Then we write

import magic

s = magic.from_file('iceland.jpg')

to call magic.from_file to read iceland.jpg and get the file’s actual MIME type as a string.

Conclusion

To check type of files without extensions with Python, we can use the magic library.

Categories
Python Answers

How to ignore ‘Incorrect padding’ error when base64 decoding with Python?

Sometimes, we want to ignore ‘Incorrect padding’ error when base64 decoding with Python.

In this article, we’ll look at how to ignore ‘Incorrect padding’ error when base64 decoding with Python.

How to ignore ‘Incorrect padding’ error when base64 decoding with Python?

To ignore ‘Incorrect padding’ error when base64 decoding with Python, we add the padding to the base64 string.

For instance, we write

base64.b64decode(s + b'==')

to call b64decode with the base64 string s concatenated with the padding string to decode the string without errors.

Conclusion

To ignore ‘Incorrect padding’ error when base64 decoding with Python, we add the padding to the base64 string.

Categories
Python Answers

How to count frequency of words in a list and sort by frequency with Python?

Sometimes, we want to count frequency of words in a list and sort by frequency with Python.

In this article, we’ll look at how to count frequency of words in a list and sort by frequency with Python.

How to count frequency of words in a list and sort by frequency with Python?

To count frequency of words in a list and sort by frequency with Python, we can use the Counter class.

For instance, we write

from collections import Counter
list1 = ['apple','egg','apple','banana','egg','apple']
counts = Counter(list1)
print(counts)

to create a Counter object with list1.

Then we get a Counter object that has the key being the item in list1 and the value being the frequency of each item.

The items are sorted by frequency in counts.

Conclusion

To count frequency of words in a list and sort by frequency with Python, we can use the Counter class.

Categories
Python Answers

How to convert int to bytes in Python?

Sometimes, we want to convert int to bytes in Python.

In this article, we’ll look at how to convert int to bytes in Python.

How to convert int to bytes in Python?

To convert int to bytes in Python, we can use the to_bytes method.

For instance, we write

b = (1024).to_bytes(2, byteorder='big')

to convert int 1024 to a byte string with to_bytes called with 2.

We specify that the byte string is big endian by setting byteorder to 'big'.

Conclusion

To convert int to bytes in Python, we can use the to_bytes method.

Categories
Python Answers

How to get a random value from dictionary with Python?

Sometimes, we want to get a random value from dictionary with Python.

In this article, we’ll look at how to get a random value from dictionary with Python.

How to get a random value from dictionary with Python?

To get a random value from dictionary with Python, we can use the random.choice method.

For instance, we write

import random

d = {'VENEZUELA':'CARACAS', 'CANADA':'OTTAWA'}
c = random.choice(list(d.values()))

to call random_choice with the list of dict values that we get from dict d with list(d.values()) to return a random dict value from d.

Conclusion

To get a random value from dictionary with Python, we can use the random.choice method.