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
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,138 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Reading an ASCII text file and parsing data... oradba4u 2 1,444 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,330 May-03-2024, 07:23 AM
Last Post: Pedroski55
Sad problems with reading csv file. MassiJames 3 2,584 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 2,109 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  How do I read and write a binary file in Python? blackears 6 25,277 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 2,106 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,954 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 1,857 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 1,679 Dec-11-2022, 07:00 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