Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python write binary

import pickle


class Person:

   def __init__(self, name, age):
       self.name = name
       self.age = age

   def get_name(self):
       return self.name

   def get_age(self):
       return self.age


person = Person('James', 21)
print(person.get_name())
print(person.get_age())

# write person object to file
with open('james', 'wb') as f:
   pickle.dump(person, f)

# read person from file
with open('james', 'rb') as f2:
   james = pickle.load(f2)

print(james.get_name())
print(james.get_age())
Comment

write binary file in python

file = open("sample.bin", "wb")
COPYfile.write(b"This binary string will be written to sample.bin")
COPYfile.close()
Comment

PREVIOUS NEXT
Code Example
Python :: list comprehension python with condition 
Python :: importing database in dataframe using sqlalchemy 
Python :: pandas categorical to numeric 
Python :: gematria python 
Python :: python del 
Python :: python custom sort 
Python :: pandas read excel with two headers 
Python :: python dict for k v 
Python :: python one line if statement without else 
Python :: python email 
Python :: python replace nth occurrence in string 
Python :: python function get number of arguments 
Python :: django regexvalidator example 
Python :: iterate backwards through a list python 
Python :: how to set variable to null in python 
Python :: validity of password in python 
Python :: python binary string to int 
Python :: how to make a button in python turtle 
Python :: pandas dataframe column based on another column 
Python :: python dict get random key 
Python :: how to import opencv in python 
Python :: python substring count 
Python :: display array of odd rows and even columns in numpy 
Python :: python how to add up all numbers in a list 
Python :: python opencv subtract two images 
Python :: get_dummies 
Python :: Django custome login 
Python :: pyqt5 image center 
Python :: how to extract words from string in python 
Python :: how to write a script to display an image in python 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =