简体   繁体   中英

Error in writing to a file (Python)

I made a program that outputs random binary characters. I wanted to modify it so it writes to a text file

print("Random Binary")
import random
file_ob = open('C:/Users/AV/Documents/Python/Binary.txt', 'w')
count = int(input("Enter the number of characters needed: "))
binary = [0]*8
counter = 1
while counter <= count:
    cnum = 0
    while cnum < 8:
            binary[cnum] = random.randint(0, 1)
            file_ob.write(print(str(binary[cnum]), end = ''))
            cnum += 1
    file_ob.write(print(end = ' '))
    counter += 1

But the IDLE gives me this error:

Traceback (most recent call last):   
File "C:/Users/AV/Documents/Python/Random Bin Write.py", line 11, in <module>   
file_ob.write(print(str(binary[cnum]), end = ''))   
TypeError: must be str, not None

I tried many things such as removing the str , removing print and a lot others, but it still gives me some error anyway... Please help.

file_ob.write('{}'.format(binary[cnum]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM