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)
浙公网安备 33010602011771号