写入文件时出错(Python)

时间:2014-01-08 12:25:20

标签: python file printing

我制作了一个输出随机二进制字符的程序。我想修改它以便写入文本文件

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

但是IDLE给了我这个错误:

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

我尝试了很多内容,例如移除str,移除print以及其他许多内容,但无论如何它仍然会给我一些错误...请帮忙。

1 个答案:

答案 0 :(得分:0)

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