程序在循环中运行,并为信息列表中列出的记录数生成文件。但是对于该列表中的最后一条记录,它会生成一个空白文件。 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
答案 0 :(得分:0)
正如DeepSpace建议你应该总是使用open()
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