Python3写入文件常用方法实例分析

yipeiwu_com6年前Python基础

本文实例讲述了Python3写入文件常用方法。分享给大家供大家参考。具体如下:

''''' 
Created on Dec 18, 2012 
写入文件 
@author: liury_lab 
''' 
# 最简单的方法
all_the_text = 'hello python'
open('d:/text.txt', 'w').write(all_the_text)
all_the_data = b'abcd1234'
open('d:/data.txt', 'wb').write(all_the_data)
# 更好的办法
file_object = open('d:/text.txt', 'w') 
file_object.write(all_the_text)
file_object.close()
# 分段写入
list_of_text_strings = ['hello', 'python', 'hello', 'world']
file_object = open('d:/text.txt', 'w')
for string in list_of_text_strings:
  file_object.writelines(string)
list_of_text_strings = ['hello', 'python', 'hello', 'world']
file_object = open('d:/text.txt', 'w')
file_object.writelines(list_of_text_strings)

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python中的闭包详细介绍和实例

一、闭包 来自wiki: 闭包(Closure)是词法闭包(Lexical Closure)的简称,是引用了自由变量的函数。这个被引用的自由变量将和这个函数一同存在,即使已经离开了创造它...

合并百度影音的离线数据( with python 2.3)

四种格式的解析: filelist slicelist download.cfg third_party_download.cfg 还是2个文件。替换之前版本即可。 初步测试正常,但时间...

Python编写检测数据库SA用户的方法

本文讲述一个用Python写的小程序,用于有注入点的链接,以检测当前数据库用户是否为sa,详细代码如下: # Code by zhaoxiaobu Email: little.bu@...

python实现文件快照加密保护的方法

本文实例讲述了python实现文件快照加密保护的方法。分享给大家供大家参考。具体如下: 这段代码可以对指定的目录进行扫描,包含子目录,对指定扩展名的文件进行SHA-1加密后存储在cvs文...

python实现dict版图遍历示例

复制代码 代码如下:#_*_coding:utf_8_import sysimport os class Graph():    def __init__(...