0

我有时遇到一个非常奇怪的问题int(),即使它真的只是数字,我也会收到错误消息

“[...] 在第 2 行:Base 10 的无效文字:''”

但是值只是一个数字(0)太奇怪了……下面的代码导致了这个错误:

count = open('count.dat', 'r')
cint = int(count.read)
cint = cint + 1
count.close()
del(count)
countw = open('count.dat', 'w+')
countw.write = str(cint)
countw.close()
del(countw)

PS:我是新手

4

1 回答 1

1

我想你的代码cint = int(count.read)countw.write = str(cint)这些行应该改变

count = open('count.dat', 'r')
cint = int(count.read()) # as this 
cint = cint + 1
count.close()
del(count)

countw = open('count.dat', 'w')
countw.write(str(cint)) #as this
countw.close()
del(countw)

然后它会正常工作

在运行代码之前

在此处输入图像描述

运行后 在此处输入图像描述

如果你 count.dat 文件显然是空白的,它会给出错误 int() with base 10: ''

于 2018-03-12T19:25:13.063 回答