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
#3
I agree with @DeaD_EyE in that the "Magic" number will probably get you the most 'positive' results. Some things to remember though, the Magic number is not a fixed length. Some may be 3 bytes, others 8, for example. Some files have no Magic number, a good example is the "Plain Text" file (think Windows Notepad). Our beloved Python files (.py) have no Magic number, as they are, after all, plain text files.  You could get some positive results if you tested for "#! /", but of course that is only if the author included the shebang line as the first line of the file.

Quote:... Finally, how do i check the value and find its type and print it out, please?

That depends on if you are looking for one specific file type, a group of similar file types or all file types. For a specific type, you could just assign the number to a variable, for a group, you would probably want a dictionary, for all types you would probably be best served with a database.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
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 sparkz_alot - Nov-22-2017, 03:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using dictionary to find the most sent emails from a file siliusu 6 9,778 Apr-22-2021, 06:07 PM
Last Post: siliusu
  Can we store value in file if we open file in read mode? prasanthbab1234 3 3,604 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  [split] how to read a specific row in CSV file ? laxmipython 2 10,660 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 9,759 May-15-2020, 04:57 PM
Last Post: snippsat
  read from file mcgrim 16 9,266 May-14-2019, 10:31 AM
Last Post: mcgrim
  Read directly from excel file using python script dvldgs05 0 2,754 Oct-19-2018, 02:51 AM
Last Post: dvldgs05
  Read a data from text file (Notepad) Leonzxd 24 18,899 May-23-2018, 12:17 AM
Last Post: wavic
  Homework - Read from/Write to file (renamed from Help help help) Amitkafle 1 3,695 Jan-11-2018, 07:24 AM
Last Post: wavic
  Cannot read from text file aljonesy 5 4,758 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