1.判断文件是否存在,存在就删除,不存在就创建等
import os
# 文件夹
path = 'D:\\result'
if not os.path.exists(path):
os.makedirs(path)
file = 'D:\\result\\res.txt'
if os.path.exists(file):
print ("file have been existed , del firstly")
os.remove(file)
2.写入文本文件
# 这个与简单的io open close的区别是后者是在内存里边写边存的,必须要close文件,否则可能容易造成数据丢失
# r 是只读,文件不存在报错,r+是读写
# w 是只写,覆盖,文件不存在会创建,w+是读写
# a 是只写,追加,文件不存在会创建,a+是读写
import codecs
with codecs.open(file,'a+',encoding='utf-8') as f:
f.write(df.ip[i]+','+station+','+df.big[i]+','+df.small[i]+','+'正常联网'+'\n')
3.pandas读写文件,见另一篇博文
4.待续