为什么python open()写不了文件?文件打开后一片空白,close()后也没有用。源代码如下:
def event(dirpath,name):
#./user/XXXX/XX/XX/...目录下的统计文件
alldir=open(dirpath+'//eventall.txt','w+',encoding='UTF-8')
if alldir.read() == '':
alldir.write('0,0')
alldir.close()
del alldir
alldir=open(dirpath+'//eventall.txt','w+',encoding='UTF-8')
r=alldir.read()
print(r)
alldir.write(str(sum(list(map(float,r.split(','))))+list(float(name)))+',0')
alldir.close()
del alldir
event('整理','4')
运行后报错:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\新建文本文档 (2).py", line 14, in <module>
event('整理','4')
File "C:\Users\Administrator\Desktop\新建文本文档 (2).py", line 11, in event
alldir.write(str(sum(list(map(float,r.split(','))))+list(float(name)))+',0')
ValueError: could not convert string to float: ''
但是后来才发现是文件一片空白,大家能帮忙看一下吗