Python Cookbook学习记录 ch2_2_2013/10/27

2.2写入文件

a.将一个长字符串写入文件:

open('thefile.txt', 'w').write(all_the_text)  # text to a text file
open('abinfile', 'wb').write(all_the_data)    # data to a binary file

b.先付给一个对象,处理完毕后在关闭文件

file_object = open('thefile.txt', 'w')
file_object.write(all_the_text)
file_object.close()

c.通过一个字符串列表逐行写

file_object.writelines(list_of_text_strings)
open('abinfile', 'wb').writelines(list_of_data_strings)

 

posted on 2013-10-27 17:24  七海之风  阅读(119)  评论(0)    收藏  举报

导航