python 读写函数
1.open
使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
2、读文件
input = open('data', 'r')
#第二个参数默认为r
input = open('data')
读二进制文件
input = open('data', 'rb')
读取所有内容
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
读固定字节
file_object = open('abinfile', 'rb')
try:
while True:
chunk = file_object.read(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_object.close( )
读每行
list_of_all_the_lines = file_object.readlines( )
如果文件是文本文件,还可以直接遍历文件对象获取每行:
for line in file_object:
process line
3.写文件
写文本文件
output = open('data', 'w')
写二进制文件
output = open('data', 'wb')
追加写文件
output = open('data', 'w+')
写数据
file_object = open('thefile.txt', 'w')
file_object.write(all_the_text)
file_object.close( )
写入多行
file_object.writelines(list_of_text_strings)
注意,调用writelines写入多行在性能上会比使用write一次性写入要高
python 读写函数的更多相关文章
- python的re模块一些方法 && Tkinter图形界面设计 && 终止python运行函数 && python读写文件 && python一旦给字符串赋值就不能单独改变某个字符,除非重新给变量赋值
Tkinter图形界面设计见:https://www.cnblogs.com/pywjh/p/9527828.html#radiobutton 终止python运行函数: 采用sys.exit(0)正 ...
- Python:pandas(一)——常用、读写函数read_csv、read_excel、to_csv、to_excel
学习自:pandas1.2.1documentation 0.常用 1)读写 ①从不同文本文件中读取数据的函数,都是read_xxx的形式:写函数则是to_xxx: ②对前n行感兴趣,或者用于检查读进 ...
- python读写Excel文件的函数--使用xlrd/xlwt
python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket The ...
- Python读写文件
Python读写文件1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('t ...
- python 读写、创建 文件
python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...
- [Python]读写文件方法
http://www.cnblogs.com/lovebread/archive/2009/12/24/1631108.html [Python]读写文件方法 http://www.cnblogs.c ...
- 第三章:Python基础の函数和文件操作实战
本課主題 Set 集合和操作实战 函数介紹和操作实战 参数的深入介绍和操作实战 format 函数操作实战 lambda 表达式介绍 文件操作函数介紹和操作实战 本周作业 Set 集合和操作实战 Se ...
- 使用Python读写csv文件的三种方法
Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是 ...
- python读写word、excel、csv、json文件
http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...
随机推荐
- Data Manipulation with dplyr in R
目录 select The filter and arrange verbs arrange filter Filtering and arranging Mutate The count verb ...
- 洛谷P2158 [SDOI2008]仪仗队 欧拉函数的应用
https://www.luogu.org/problem/P2158 #include<bits/stdc++.h> #define int long long using namesp ...
- 连接本地mysql报错
报错信息如下: 原因为未启动本地mysql,没设置开机自启动.
- 题解【BZOJ4472】[JSOI2015]salesman
题面 树形\(\text{DP}\)与贪心的结合. 首先考虑树形\(\text{DP}\). 设\(dp_i\)表示从\(i\)出发,访问\(i\)的子树,并且最后回到\(i\)能获得的最大收益. 转 ...
- 题解 洛谷 P4145 【上帝造题的七分钟2 / 花神游历各国】
题目 上帝造题的七分钟2 / 花神游历各国 题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. ...
- 利用Cadence PCB SI分析特性阻抗变化因素
1.概要 在进行PCB SI的设计时,理解特性阻抗是非常重要的.这次,我们对特性阻抗进行基础说明之外,还说明Allegro的阻抗计算原理以及各参数和阻抗的关系. 2.什么是特性阻抗? 2.1 传送线路 ...
- nginx 定义:响应头和请求头
1) 响应头 add_header 例如: add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; a ...
- 每天进步一点点------Xilinx DCM
时钟---锁相环 1. Xilinx DCM 数字时钟管理模块(Digital Clock Manager,DCM)是基于Xilinx的其他系列器件所采用的数字延迟锁相环(DLL,Dela ...
- Linux中 /boot 目录介绍
转自https://blog.csdn.net/dulin201004/article/details/7396968 一./boot/目录中的文件和目录 Linux系统在本地启动时,目录/boot/ ...
- makecert 产出证书
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Bin>makecert -r -n // -e // -sv mymuse.pvk my ...