python3:文件读写+with open as语句
转载请表明出处:https://www.cnblogs.com/shapeL/p/9141238.html
前提:文中例子介绍test.json内容:
hello
我们
326342
1.文件读取
(1)打开文件open,默认是已读模式打开文件
f = open('../dataconfig/test.json')
print(f.read())
f.close()
输出结果:
hello
鎴戜滑
326342
read():一次性读取文件所有内容
输出结果中出现乱码:需要给open函数传入encoding参数
f = open('../dataconfig/test.json',encoding='utf-8')
print(f.read())
f.close()
输出结果:
hello
我们
326342
(2)read(size):读取部分数据
f = open('../dataconfig/test.json',encoding='utf-8')
print(f.read(3))
f.close()
输出结果: hel
(3)redline():每次读取一行数据,逐行读取文件内容
f = open('../dataconfig/test.json',encoding='utf-8')
data = f.readline()
while data:
print(data)
data = f.readline()
f.close()
输出结果:
hello
我们
326342
输出结果中每一行数据读取之后都会空一行,解决方案:print(data.strip())或者print(data,end='')
(4)readlines():读取文件所有行
f = open('../dataconfig/test.json',encoding='utf-8')
data = f.readlines()
print(data)
print(type(data))
for line in data:
print(line.strip())
f.close()
输出结果:
['hello\n', '我们\n', '']
<class 'list'>
hello
我们
326342
(5)linecache.getline():读取某个特定行数据
import linecache
data = linecache.getline('../dataconfig/test.json',1)
print(data)
输出结果:
hello
总结:不同场景下读取方式选择
f = open('../dataconfig/test.json','w')
f.write('hello,world!')
f.close()
test.json文件内容:hello,world!
(2)‘’a’就是appendin:一种写入模式,写入的内容不会覆盖之前的内容,而是添加到文件中
f = open('../dataconfig/test.json','a')
f.write('hello,world!')
f.close()
test.json文件内容:
hello
我们
326342hello,world!
3.上述读写文件例子看出,每次读写完之后,都要f.close()关闭文件,因为文件对象会占用操作系统的资源,并且操作系统同一时间能打开的文件数量也是有限的。
但是实际中,文件读写可能产生IOError,一旦出错,后面的f.close()就不会调用。所以,为了保证任何时候都能关闭文件,可以使用try-finally来实现(finally内的代码不管有无异常发生,都会执行)
try:
f = open('../dataconfig/test.json', 'r')
print(f.read())
finally:
if f:
f.close()
每次都这样写实在是麻烦,python中的with语句用法可以实现
with open('../dataconfig/test.json',encoding='utf-8') as f:
print(f.read())
输出结果:
hello
我们
326342
打开多个文件进行操作:
with open('../dataconfig/test.json',encoding='utf-8') as f1,open('../dataconfig/test1.json',encoding='utf-8') as f2,open('../dataconfig/test2.json',encoding='utf-8') as f3:
for i in f1:
j = f2.readline()
k = f3.readline()
print(i.strip(),j.strip(),k.strip())
python3:文件读写+with open as语句的更多相关文章
- python3:文件读写+with open as语句(转)
读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...
- python3 文件读写,编码错误UnicodeDecodeError
问题:python3 with open文件进行读写,报编码错误 /usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Version ...
- Python3 文件读写r,w,a
# Author;Tsukasa ''' f = open('yesterday','w') #文件句柄...注意open分为‘r’读模式,‘w’写模式(d会先创建文件或者覆盖文件),‘a’为追加模式 ...
- python3 文件读写操作中的文件指针seek()使用
python中可以使用seek()移动文件指针到指定位置,然后读/写.通常配合 r+ .w+.a+ 模式,在此三种模式下,seek指针移动只能从头开始移动,即seek(x,0) . 模式 默认 写方式 ...
- Python3 文件读写注意事项(指针问题)
C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe E:/python/day2/op.py Someho ...
- python文件读写,以后就用with open语句
读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...
- Python3:文件读写
Python3:文件读写 open f = open('filename','r') # 读模式 f = open('filename','w') # 写模式 f = open('filename', ...
- Python3 IO编程之文件读写
读写文件是最常见的IO操作.python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一个,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序终结操作磁盘, ...
- python3的文件读写模式
任何一种语言,文件的读写都是非常常见的.python的文件读写非常简单,仅仅一个函数open(file也可以,但是我不常用). 先看看官网的解释: open(file, mode='r', buffe ...
随机推荐
- ng-model 数据不更新 及 ng-repeat【ngRepeat:dupes】错误
一.ng-include 引入的文件中 ,ng-model 数据不更新 例如, $scope.username = “Jones” .此时,在 ng-include 引入的文件中,直接使用 ng-m ...
- 洛谷P2777 [AHOI2016初中组]自行车比赛
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- python flask demo
from flask import Flask, jsonify from flask import abort from flask import make_response from flask ...
- JavaScript的知识基本介绍
ECMAScript js简单介绍(与java的区别) 1.语法(区分大小写,弱类型,分号可写可不写) 2.变量(只能使用var定义,要么不定义,如果在函数内部使用var定 ...
- spring boot: thymeleaf模板引擎使用
spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...
- (GoRails )使用Vue.js制作拖拉list功能(v1-4) gem 'acts_as_list'(自动排列顺序)
系列视频: use Vue.js to build the drag and drop support for the list themselves the cards that are under ...
- IE6不兼容hover已解决
新建一个csshover.htc文件,一下是csshover.htc内容 <public:attach event="ondocumentready" onevent=&qu ...
- 原创-整理了下常用的js数组 、对象、数字、字符串的操作方法
终于整理好了...主要是一些常用的函数,包含es6和es5的所有常用的,吧一些不常用的全部砍掉,省的大家看的费事.发现这个到博客上面有点乱.给个百度云地址:https://pan.baidu.com/ ...
- C#图片转换成二进制流并且保存到sql server数据库
注意:我要存储文件二进制流的列的类型是text,不是image类型. 我已经实现了从数据库中读取text类型的二进制流,,现在就是不知道怎么存进去. 我的部分关键代码: StreamReader sr ...
- synchronized锁普通方法和锁静态方法
1.对象锁钥匙只能有一把才能互斥,才能保证共享变量的唯一性 2.在静态方法上的锁,和 实例方法上的锁,默认不是同样的,如果同步需要制定两把锁一样. 3.关于同一个类的方法上的锁,来自于调用该方法的对象 ...