Nov-22-2017, 01:53 PM
Often it's much more complicated to guess a file type related to the content. Many file formats do have magic number at the beginning. If you want to get the first 3 bytes, then just read the first 3 bytes:
If you want just use a library/wrapper for it, you should look for this modules:
file-magic # official
filemagic
More about Magic numbers.
There are different ways to detect if a given sub string (bytes) is in a string.
If you want to detect PNG, you need for example 8 bytes.
with open('YourFile.ext', 'rb') as fd: file_head = fd.read(3)file_head has now the first 3 bytes. You can make a predefined table for comparison.
If you want just use a library/wrapper for it, you should look for this modules:
file-magic # official
filemagic
More about Magic numbers.
There are different ways to detect if a given sub string (bytes) is in a string.
If you want to detect PNG, you need for example 8 bytes.
magic_numbers = {'png': bytes([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])} max_read_size = max(len(m) for m in magic_numbers.values()) # get max size of magic numbers of the dict with open('YourFile.png', 'rb') as fd: file_head = fd.read(max_read_size) if file_head.startswith(magic_numbers['png']): print("It's a PNG File") else: print("It's not a png file")It's just an example how you can do it.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!