Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

Writing to binary file in Python

I am trying to write 0 and 1 to a binary file. I have a program which prints 0 or 1 based on the condition is either true or false.

I am opening the file for writing with wb

f = open("output", "wb")
count=0
result=0
testv=10
while(count <= 1000000):
     testv=testv**count
    if(count< 59980):
        result=0
    else:
        result=1
    
    f.write(str(result))
    count=count+1
f.close()

When i open the file in Hex editor it shows something like "30313131 31313130 30313131 30303031 30313031" which I assume is not a binary file with just 0 and 1.

How can I write a file in python which is just plain binary 0 and 1?

question from:https://stackoverflow.com/questions/65917289/writing-to-binary-file-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...