Python 3.x ValueError:以10为基数的int()的文本无效:'';[不是浮动]

Python 3.x ValueError:以10为基数的int()的文本无效:'';[不是浮动],python-3.x,Python 3.x,我试图制作一个python程序,将数字保存到文件中,然后读取它。(不是浮点)但当我尝试将其转换为int时,它会打印: ValueError: invalid literal for int() with base 10: '' 有什么帮助吗 代码片段: ... t= open("Bot","w+") t.write('%d' % random.randint(1,100)) t.close() ... t= open("Bot","w+") num=t.read() print(int(num

我试图制作一个python程序,将数字保存到文件中,然后读取它。(不是浮点)但当我尝试将其转换为int时,它会打印:

ValueError: invalid literal for int() with base 10: ''
有什么帮助吗

代码片段:

...
t= open("Bot","w+")
t.write('%d' % random.randint(1,100))
t.close()
...
t= open("Bot","w+")
num=t.read()
print(int(num))
...
t= open("Bot","w+")
t.write('%d' % random.randint(1,100))
t.close()
...
t= open("Bot","r") # <- Here
num=t.read()
print(int(num))

我发现我必须使用r参数。很抱歉 完整的代码片段:

...
t= open("Bot","w+")
t.write('%d' % random.randint(1,100))
t.close()
...
t= open("Bot","w+")
num=t.read()
print(int(num))
...
t= open("Bot","w+")
t.write('%d' % random.randint(1,100))
t.close()
...
t= open("Bot","r") # <- Here
num=t.read()
print(int(num))
。。。
t=开放(“机器人”,“w+”)
t、 写入(“%d”%random.randint(1100))
t、 关闭()
...

t=open(“Bot”,“r”)#如果需要读取文件,则必须使用
t=open(“Bot”,“r”)
-