#-*-coding:utf-8-*-
'''
Created on 2015年6月22日
@author: Zroad
'''
"""
文件写入模式:
1、常用的写入函数为write(str),writelines(list[str])
2、写入的必须是字符串,writelines()写入字符串列表
3、写入的内容不会自动添加换行符,必须显示写入换行符
"""
file_write_object = open("text.abc",'a+')
all_the_text = "Hello,python;My name is zroad,I will be perfect pythoner!\n"
list_of_text_strings = ["This",'is','HuHu','\n']
try:
file_write_object.write(all_the_text) #写入大字符串
file_write_object.write(str(1)+'\n') #非字符串类型转换为字符串,才能写入
file_write_object.write(str([1,2,3])+'\n')
file_write_object.writelines(list_of_text_strings) #写入字符串列表
finally:
file_write_object.close()
file_read_object = open("text.abc",'rU')
try:
all_texts = file_read_object.read()
print "all_texts",all_texts
finally:
file_read_object.close()
文件对象-写操作
最新推荐文章于 2023-07-05 20:00:00 发布