Python Forum
read a binary file to find its type
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read a binary file to find its type
#5
It should be enuff to test for b'\xff\xd8\xff',then it's a jpg.
Start bits:
$JPEG = "\xFF\xD8\xFF"
$GIF  = "GIF"
$PNG  = "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
$BMP  = "BM"
$PSD  = "8BPS"
$SWF  = "FWS"
with open('some.gif', 'rb') as fd:
    file_head = fd.read(3)
print(file_head)
Output:
b'GIF'
atux_nul Wrote:and 255, 216, 255 in integers
>>> s
b'\xff\xd8\xff'
>>> [i for i in s]
[255, 216, 255]
You see on wiki page that it decode to ÿØÿÛ using ISO 8859-1.
>>> s
b'\xff\xd8\xff'
>>> s.decode('iso-8859-1')
'ÿØÿ'
Reply


Messages In This Thread
read a binary file to find its type - by atux_null - Nov-22-2017, 12:35 PM
RE: read a binary file to find its type - by snippsat - Nov-23-2017, 09:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using dictionary to find the most sent emails from a file siliusu 6 7,703 Apr-22-2021, 06:07 PM
Last Post: siliusu
  Can we store value in file if we open file in read mode? prasanthbab1234 3 2,637 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  [split] how to read a specific row in CSV file ? laxmipython 2 8,969 May-22-2020, 12:19 PM
Last Post: Larz60+
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 7,098 May-15-2020, 04:57 PM
Last Post: snippsat
  read from file mcgrim 16 6,320 May-14-2019, 10:31 AM
Last Post: mcgrim
  Read directly from excel file using python script dvldgs05 0 2,296 Oct-19-2018, 02:51 AM
Last Post: dvldgs05
  Read a data from text file (Notepad) Leonzxd 24 14,233 May-23-2018, 12:17 AM
Last Post: wavic
  Homework - Read from/Write to file (renamed from Help help help) Amitkafle 1 3,088 Jan-11-2018, 07:24 AM
Last Post: wavic
  Cannot read from text file aljonesy 5 3,682 Oct-05-2017, 05:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020