0

How can i get file extension from base64 string? OR How can i get file extension from bytes object?

def get_extension_from_base(base):
    decode_base = base.encode('ascii')
    decode_base = base64.b64decode(decode_base)

    extension =  SOMETHING_HEAR(decode_base)
    
    return extension

Python 3.8

  • 2
    Base64 is just an encoding of the data and does not contain any metadata like filename or extension. But you can figure out what kind of data is encoded in a base64 string by comparing the first few characters as shown [here](https://stackoverflow.com/questions/62329321/how-can-i-check-a-base64-string-is-a-filewhat-type-or-not/62330081#62330081) – jps Dec 01 '21 at 14:26
  • File extension is usually not contained in file data. Do you want to guess the **type** (or data format) of the file? – Michael Butscher Dec 01 '21 at 14:27
  • 1
    Does this answer your question? [How to check type of files without extensions?](https://stackoverflow.com/questions/10937350/how-to-check-type-of-files-without-extensions) – Reti43 Dec 01 '21 at 15:13
  • I need the type of file, but i dont have the file. Just the base64. – pedro Henrique Assumpção Dec 02 '21 at 12:49
  • Thanks @jps. Thats it. I think that aswer can be a library in future. – pedro Henrique Assumpção Dec 02 '21 at 12:50

0 Answers0