1、使用open与print进行数据保存到文件
#filename列表形式文件名
def write_file(filename):
try:
for item_name in filename:
out_file=open(item_name,'w') #写模式打开文件,并赋值至文件对象
data='this is'+item_name
print(data,file=out_file) #将data数据保存到指定文件
out_file.close() #关闭文件
except IOError:
print('file error!')
if __name__=='__main__':
write_file(['C:/Users/Administrator/Desktop/out.txt'])
2、上述代码改进,有一些代码不论出现什么错误都必须执行,可以向try语句的finally组增加代码
#filename列表形式文件名
def write_file(filename):
#存放输出文件
out_file_list=[]
try:
for item_name in filename:
out_file=open(item_name,'w')
data='this is'+item_name
print(data,file=out_file)
out_file_list.append(out_file)
except IOError:
print('file error!')
finally:
for out_file_item in out_file_list:
out_file_item.close()
if __name__=='__main__':
write_file(['C:/Users/Administrator/Desktop/out.txt'])
[^使用try/except/finally模式处理文件相当常用]:
3、使用with处理文件,使用with不需要再使用finally组处理文件关闭,python解释器会自动为你考虑。
#filename列表形式文件名
def write_file_use_with(filename):
#存放输出文件
out_file_list=[]
try:
for item_name in filename:
with open(item_name,'w') as out_file:
data='this is'+item_name
print(data,file=out_file)
out_file_list.append(out_file)
except IOError as err:
print('file error:'+str(err))
if __name__=='__main__':
write_file_use_with(['C:/Users/Administrator/Desktop/out.txt'])
[^with语句利用一种名为上下文管理协议的python技术]:
4、修改文件写的格式,重新定义print写的方法
#按格式输出列表,若遇见列表进行缩进。
#indent:是否进行缩进
#level:缩进几个制表符
#fn:文字输出位置(默认屏幕打印)
def print_handle_list(the_list,indent=False,level=0,fn=sys.stdout):
for item in the_list:
if isinstance(item,list):
print_handle_list(item,indent,level+1,fn)
else:
if indent:
for tab in range(level):
print("\t",end='',file=fn)
print(item,file=fn)
#filename列表形式文件名
def write_file_use_with(filename):
#存放输出文件
out_file_list=[]
try:
for item_name in filename:
with open(item_name,'w') as out_file:
data='this is'+item_name
print_handle_list(data,fn=out_file) #调用新的print方法
out_file_list.append(out_file)
except IOError as err:
print_handle_list('file error:'+str(err))
5、python提供了一个标准库,名称为pickle,它可以保存和加载几乎任何python数据对象。
6、使用dump保存数据(必须要以二进制访问模式打开文件)-“腌制“:将数据对象保存到一个持久存储中的过程
#引入pickle模块
import pickle
#filename列表形式文件名
def write_file_use_pickle(filename):
#存放输出文件
out_file_list=[]
try:
for item_name in filename:
#wb:写二进制模式
with open(item_name,'wb') as out_file:
data=['this is'+item_name,'test_pickle']
pickle.dump(data,out_file) #调用pickle的dump方法
out_file_list.append(item_name)
except IOError as err:
print('file error:'+str(err))
except pickle.PickleError as perr:
print('pickling error:'+str(perr))
print(out_file_list)
if __name__=='__main__':
write_file_use_pickle(['C:/Users/Administrator/Desktop/out.txt'])
7、使用load恢复数据(必须要以二进制访问模式打开文件)-”解除腌制“:从持久存储中恢复一个已保存的数据对象的过程
#引入pickle模块
import pickle
#filename列表形式文件名
def read_file_use_pickle(filename):
try:
for item_name in filename:
#wb:写二进制模式
with open(item_name,'rb') as out_file:
data=pickle.load(out_file)
print_handle_list(data)
except IOError as err:
print('file error:'+str(err))
except pickle.PickleError as perr:
print('pickling error:'+str(perr))
if __name__=='__main__':
read_file_use_pickle(['C:/Users/Administrator/Desktop/out.txt'])
喜欢我的文章希望和我一起成长的宝宝们,可以搜索并添加公众号TryTestwonderful ,或者扫描下方二维码添加公众号