代码如下:
#encoding=utf-8 print '中国' #写入文件 #方案一 注意 close才能保证数据写入 file_object = open(r'd:\123.txt','w') file_object.write('123abc中国') file_object.close() file_object = open(r'd:\1234.txt','wb') file_object.write('123abc中国') file_object.close() #写入列表字符串 listofstring=['123','abc','中国','编码'] file_object = open(r'd:\1235.txt','w') file_object.writelines(listofstring) file_object.close()
打印结果如下:
中国
#都能成功写入