Python Forum
reading the binary contents of any file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading the binary contents of any file
#1
I was wondering how to make it so I can load the binary of any file
so say I have a file called,
file1.qra
(qra is made up)
and the binary contents of it are:
011000010110001001100011
I wanna be able to load the binary
contents in any file to python,
and with file1.qra
it would return a string:
011000010110001001100011

so, how do I do this?
Reply
#2
just open the file as mode 'rb'
Reply
#3
so i have the file:
file1.txt
and its contents are:
abc
this is what i did
with open(file1.txt, 'rb') as f: # Open data input file
     f_contents = f.read()
     print(f_contents)
and it gave me this:
b'abc'
when I printed f_contents
Reply
#4
Quote:load the binary of any file
I took above to mean you wanted to open the file in binary mode.
I think what you're saying is that you want to display the contents of a text file as binary.
Coorrect?
Reply
#5
(Sep-15-2018, 07:18 PM)qrani Wrote: I was wondering how to make it so I can load the binary of any file
so say I have a file called,
file1.qra
(qra is made up)
and the binary contents of it are:
011000010110001001100011
I wanna be able to load the binary
contents in any file to python,
and with file1.qra
it would return a string:
011000010110001001100011

so, how do I do this?

Read a file content in a binary mode - and the convert every byte with a secret Snooty (just kidding) bin function
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 643 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 918 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  How do I read and write a binary file in Python? blackears 6 6,625 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,108 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,095 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 990 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 899 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 989 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Failing reading a file and cannot exit it... tester_V 8 1,822 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Reading .csv file doug2019 4 1,705 Apr-29-2022, 09:55 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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