现在的位置: 首页 > 综合 > 正文

002_002 Python 写入文件

2018年02月15日 ⁄ 综合 ⁄ 共 372字 ⁄ 字号 评论关闭

代码如下:

#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()

打印结果如下:

中国

#都能成功写入

抱歉!评论已关闭.