微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python3写入文件常用方法实例分析

本文实例讲述了python3写入文件常用方法分享给大家供大家参考。具体如下:

''''' 
Created on Dec 18,2012 
写入文件 
@author: liury_lab 
''' 
# 最简单的方法
all_the_text = 'hello python'
open('d:/text.txt','w').write(all_the_text)
all_the_data = b'abcd1234'
open('d:/data.txt','wb').write(all_the_data)
# 更好的办法
file_object = open('d:/text.txt','w') 
file_object.write(all_the_text)
file_object.close()
# 分段写入
list_of_text_strings = ['hello','python','hello','world']
file_object = open('d:/text.txt','w')
for string in list_of_text_strings:
  file_object.writelines(string)
list_of_text_strings = ['hello','w')
file_object.writelines(list_of_text_strings)

希望本文所述对大家的Python程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐