由程序创建的空白文件
问题描述:
该程序运行在一个循环中,并为信息列表中列出的记录数生成文件。但是对于该列表中的最后一条记录,它将生成一个空白文件。 final_list永远不是空白的,我检查,但仍然每次最后一个正在创建的文件是空的。有人可以告诉我做错了吗?由程序创建的空白文件
outputC = net_connect.send_command("show int desc | i xe")
outputC1 = net_connect.send_command("show int desc | i xt")
final_list = capture_op(outputC,outputC1)
length = len(info[count][1])
name = info[count][1]
name = name[0:length]+".txt"
for path,dirname,filename in os.walk("."):
for file in filename:
if file == name:
os.remove(name)
break
fo=open(name , 'w')
for i in final_list:
print (i)
fo.write(i)
fo.write('\n')
fo.close
count+=1
答
由于DeepSpace建议你应该总是与开放()
with open(name, 'w') as fo:
for i in final_list:
print (i)
fo.write(i)
fo.write('\n')
count+=1
的Python参考使用: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
你不是叫'fo.close'。 这样的电话: 'fo.close()' –
谢谢,即时通讯同样的错误很多.. –
使用开放(...)作为...:'你不用担心。 – DeepSpace