十四、python沉淀之路--文件操作
一、文件操作b模式
1、
# f = open('test11.py','rb',encoding='utf-8') # 这种情况会报错
f = open('test11.py','rb') # b 模式不能指定编码方式
data = f.read()
print('直接打印出来:',data)
print('解码打印\n',data.decode('utf-8'))
f.close()
直接打印出来: b'11111\r\n22222\r\n3333\r\n'
解码打印
11111
22222
3333
总结:字符串 -------encoding ------->> bytes
bytes --------decode --------->> 字符串
例2
f = open('test22.py','wb')
f.write(bytes('99999\n',encoding='utf-8'))
f.write('北京'.encode('utf-8'))
f.close() f1 = open('test22.py','br')
data = f1.read()
print('原码打印',data)
print('解码打印\n',data.decode('utf-8'))
f1.close()
原码打印 b'99999\n\xe5\x8c\x97\xe4\xba\xac'
解码打印
99999
北京
注:原码打印和解码打印的区别,写文件前,即在打开的时候不能指定编码格式。只有在写的时候才指定编码格式。
二、文件操作
1、
f = open('练习.txt',"r+",encoding='utf-8')
print(f.read())
f.write('1234556789\n')
print(f.read())
f.close()
1234556789
2、closed,encoding, flush(),readlinse() 的使用
f = open('练习.txt',"r+",encoding='utf-8',newline='')
print(f.closed) # closed 判断文件是否关闭
print(f.encoding) # 检查是哪种编码格式
f.flush() # 写完内容后是暂时存在了缓存里面,通过flush()函数冲刷一下,就将内容保存在了内存、# f.flush() #讲文件内容从内存刷到硬盘
print(f.readlines()) #将所有内容以列表形式打印出来,
False
utf-8
['\r\n', '1234556789\r\n', '1234556789\r\n']
3、tell的使用,打印当前所在位置
f = open('练习.txt',"r+",encoding='utf-8',newline='')
print(f.tell())
print(f.readline()) # 因为12345 后还有\r\n,没有显示出来
print(f.tell())
4、seek的使用,(是以字节为单位的)
f = open('练习.txt',"r+",encoding='utf-8',newline='')
f.write('my name is cainiao who is from earth')
f.close() f1 = open('练习.txt','r+',encoding='utf-8')
print(f1.read()) f1.seek(3,0) #“0”代表从文件开头开始偏移,偏移3个单位
print(f1.tell())
print(f1.read(3))
print(f1.read(6)) #从偏移之后的指针所指的位置,开始读取6个字符
print(f1.tell()) print(f1.readline())
# print('看看是否到了最后:',f1.read(5)) print(f1.seek(5,0))
print(f1.seek(3,1))
print(f1.seek(0,2)) #“2”代表从末尾算起,“0”代表偏移0个单位
nam
print(f1.seek(3,1))
e is c
io.UnsupportedOperation: can't do nonzero cur-relative seeks
12
ainiao who is from earth
5
5、读取大文件中最后一行
f=open('练习.txt','rb')
for i in f:
offs=-10
while True:
f.seek(offs,2)
data=f.readlines()
if len(data) > 1:
print('文件的最后一行是%s' %(data[-1].decode('utf-8')))
break
offs*=2
十四、python沉淀之路--文件操作的更多相关文章
- 十三、python沉淀之路--文件操作
一.文件的读操作 例1 f = open('学习',encoding='utf-8') #首先要打开文件,不然直接读,是读不出来的 data = f.read() #read后的括号里不添加任何东西 ...
- Python学习之路——文件操作
文件操作分三步:打开文件,读写文件,关闭文件.读取操作时没有给read函数加括号,会出现下面这样的车祸 >>> data = open('/home/supersun/Documen ...
- Python修炼之路-文件操作
Python编程之文件操作 文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 每次文件打开.读取信息时,Python自动记录所达到的位置,好比一个书签,之后每一次 ...
- 十、python沉淀之路--高阶函数初识
一.高阶函数:分两种:一种是返回值中包含函数体:另一种是把一个函数体当作了参数传给了另一个函数 1.返回值中包含函数体 例1. def test(): print('这是一个测试') return t ...
- 二十四. Python基础(24)--封装
二十四. Python基础(24)--封装 ● 知识结构 ● 类属性和__slots__属性 class Student(object): grade = 3 # 也可以写在__slots ...
- python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法
python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法window安装redis,下载Redis的压缩包https://git ...
- 十四. Python基础(14)--递归
十四. Python基础(14)--递归 1 ● 递归(recursion) 概念: recursive functions-functions that call themselves either ...
- python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码
python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...
- Python之路----文件操作
文件操作 1.能调用方法的一定是对象,比如数值.字符串.列表.元组.字典,甚至文件也是对象,Python中一切皆为对象. str1 = 'hello' str2 = 'world' str3 = ' ...
随机推荐
- CSS3飘带状3D菜单
在线演示 本地下载
- 利用SSH协议在Windows下使用PuTTY连接Ubuntu
利用SSH协议在Windows下使用PuTTY连接Ubuntu Ubuntu部分 首先我们要为Ubuntu配置一下环境,让它支持ssh服务,我们要做的其实也很简单,就一下两步: 安装OpenSSH软件 ...
- Spring 是什么
- Aware接口
Aware接口: 例如: BeanNameAware接口是为了让自身Bean能够感知到,获取到自身在Spring容器中的id属性. 同理,其他的Aware接口也是为了能够感知到自身的一些属性. 比如实 ...
- 通过wifi连接android设备的方法
[转自]http://blog.csdn.net/kuanxu/article/details/7444874 最近由于要在另外一台android设备上调试代码,在本机PC上查看其log.两台机器离的 ...
- on绑定阻止冒泡失败
使用zepto库,有如下dom <div id="J_parent"> <a href="#"> <span>点我有惊喜&l ...
- RENOUNCEMENT
I must not think of thee;and,tired yet syrong,I shun the thought that lurks in all delight--The thou ...
- JQuery中选择元素的方法:
document.getElementById('div1');document.getElementsByTagName('div');getByClass(document,'box'); $(' ...
- Web API与AJAX:理解FormBody和 FormUri的WebAPI中的属性
这是这一系列文章"与 AJAX 的 Web API".在这一系列我们都解释消耗 Web API rest 风格的服务使用 jQuery ajax() 和其他方法的各种方法.您可以阅 ...
- ubuntu16.04 安装OpenNI并运行kinnectfusion
由于OpenNI是ubuntu12.04以前使用的驱动kinnect的库,现在用起来有很多的不便,用心的系统运行旧的设备,有诸多问题.现总结流程如下: 环境:Ubuntu16.04 64bit Kin ...