python学习之文件读写入门(文件读的几种方式比较)
1、文件读写简单实例:(以w写的方式打开一个文件,以r读一个文件)
# Author : xiajinqi # 文件读写的几种方式
# 文件读写
f = open("D://test.txt","w",encoding="utf-8")
f.write("hello world")
f.flush()
f.close() f = open("D://test.txt","r",encoding="utf-8")
data = f.read()
data2 = f.read()
f.seek(0,0)
data3 = f.read()
print("data-------------------",data)
print("data2------------------",data2)
print("data3-----------------",data3)
2、文件写的w和a 简单介绍
# Author : xiajinqi # 文件读写的几种方式
# 文件写,文件写,如果就文件存在,会清空旧文件内容(切记),如果不存在就创建。并且不能读
f = open("test.txt","w",encoding="utf-8")
f.write("hello world1\n")
f.write("hello world2\n")
f.write("hello world3\n")
f.flush()
f.close() f = open("D://test.txt","r",encoding="utf-8")
data = f.read()
print(data)
f.close() # 文件追加,不能读,在文件尾部追加,不会清空旧的文件
f = open("test.txt","a",encoding="utf-8")
f.write("追加1")
f.close() f = open("test.txt","a",encoding="utf-8")
f.write("\n追加2")
f.close() f = open("test.txt","r",encoding="utf-8")
data = f.read()
print(data)
f.close() 执行结果
E:\Users\xiajinqi\PycharmProjects\twoday\venv\Scripts\python.exe E:/Users/xiajinqi/PycharmProjects/twoday/file.py
hello world1
hello world2
hello world3 hello world1
hello world2
hello world3
追加1
追加2 Process finished with exit code 0
3、文件读r的详细使用。文件读的几种方式和优缺点:
# Author : xiajinqi
# 文件读按照行数读
#方式一,读全部内容(文件太大时候,内存会爆掉)
f = open("test.txt","r",encoding="utf-8")
print("一次性读出来--------------")
print(f.read())
f.close() #方式2,一行一行读readline ,读出所有的行,并且转换为数组f.readlines()
#由于一直在往内存读,导致内存会爆掉,这种循环又称为low
f = open("test.txt","r",encoding="utf-8")
print("一行一行读low looper--------------")
for key,line in enumerate(f.readlines()):
print(key,line.strip())
f.close() # 方式3 :文件循环读。内存每次只有一行,读一行,关闭一行,内存永远不会爆掉。建议使用3,效率最高
f = open("test.txt","r",encoding="utf-8")
print("一行一行读bigger looper--------------")
for line in f :
print(line.strip())
f.close()
4、练习题目 ,实现第九行不打印的两种方式
方式一:
f = open("test.txt","r",encoding="utf-8")
print("不打印第九行")
count = 0
for line in f :
count = count + 1
if count == 9 :
print("分隔符>>>>>>>>>>>>>>>>>>>")
continue
print(line.strip())
f.close() 方式二:
f = open("test.txt","r",encoding="utf-8")
print("不打印第九行方式二")
for key,line in enumerate(f.readlines()) :
if key == 8 :
print("分隔符>>>>>>>>>>>>>>>>>>>")
continue
print(key+1,line.strip())
f.close()
5、seek(),tell() 介绍和总结:
#tell 记录当前指针的位置(字符位置),seek设置指针的位置
f = open("test.txt","r",encoding="utf-8")
print("第一次打印")
print(f.read())
print(f.read()) # 指针已经到了文件尾部,继续读将为空
print("第二次打印")
f.seek(0,0)
print(f.read())
f.close()
#查找当前位置
f = open("test.txt","r",encoding="utf-8")
f.readline()
print(f.tell())
f.close()
6、文件的其他函数总结:
#
f = open("test.txt","r",encoding="utf-8")
print("其他函数使用")
print(f.fileno()) #文件在系统中的编号,一般
print(f.name) #文件名字
print(f.seekable()) #终端设备无法移动
print(f.readable()) #文件是否可以读
f.close()
7、flush 使用。将内存数据写到文件(系统默认不是实时刷新)
import sys,time
# 显示进度条
for i in range(10) :
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.2)
python学习之文件读写入门(文件读的几种方式比较)的更多相关文章
- Python学习系列(五)(文件操作及其字典)
Python学习系列(五)(文件操作及其字典) Python学习系列(四)(列表及其函数) 一.文件操作 1,读文件 在以'r'读模式打开文件以后可以调用read函数一次性将文件内容全部读出 ...
- python使用xlrd模块读写Excel文件的方法
本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...
- python学习笔记(六)文件夹遍历,异常处理
python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...
- 使用 JavaScript 的 HTML 页面混合、JavaScript 文件引用和 HTML 代码嵌入 3 种方式在 HTML 页面上打印出“点击我进入到百度首页”的超链接
查看本章节 查看作业目录 需求说明: 使用 JavaScript 的 HTML 页面混合.JavaScript 文件引用和 HTML 代码嵌入 3 种方式在 HTML 页面上打印出"点击我进 ...
- python学习笔记(二)文件操作和集合
集合: 集合也是一种数据类型,一个类似列表东西,它的特点是无序的,不重复的,也就是说集合中是没有重复的数据 集合的作用: 1.它可以把一个列表中重复的数据去掉,而不需要你再写判断 2.可以做关系测试, ...
- python学习笔记之十:文件和素材
这里将介绍函数和对象--文件和流,让你在程序调用期间存储数据,并且可以处理来自其他程序的数据. 一. 打开文件 1.1 open函数 open函数用来打开文件,语法如下:open(name,[.mod ...
- python学习笔记——(三)文件操作
·集合操作及其相应的操作符表示集合中没有插入,只有添加,因为毕竟无序 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Vergil Zhan ...
- Python学习笔记 -- 第六章 文件操作
I/O编程 在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这 ...
- python学习day9 字符编码和文件处理
1.字符编码 x='上' #unicode的二进制--------->编码-------->gbk格式的二进制 res=x.encode('gbk') #bytes 字节类型 print( ...
随机推荐
- 鲁棒图(Robustness Diagram)
鲁棒图与系统需求分析 鲁棒图(Robustness Diagram)是由Ivar Jacobson于1991年发明的,用以回答“每个用例需要哪些对象”的问题.后来的UML并没有将鲁棒图列入UML标准, ...
- 【Leetcode】【Medium】Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 替换NSString类中的stringWithFormat:方法
替换NSString类中的stringWithFormat:方法 先给出源码: YXUseful.h // // YXUseful.h // NSString // // Copyright (c) ...
- QT的组件布局
在QT的IDE下,编写一个自定义布局. #include<QApplication> #include<QWidget> #include<QSpinBox> #i ...
- Python学习---重点模块之pickle
仅仅支持Python里面的函数等相关功能的实现,而且pickle写入的内容是看不出来的,读取的时候要求有原内容 pickled的写入: import pickle def fun(): print(' ...
- postgraSql支持View可以修改的两种方法。
http://www.postgresqltutorial.com/postgresql-views/ Creating PostgreSQL updatable views – gives you ...
- ZT 骆家辉宣布辞职 他给中国带来什么留下什么?
骆家辉宣布辞职 他给中国带来什么留下什么? 字号|2013年11月20日 15:20 已有1933人阅读 57 导 读 美国驻华大使骆家辉20日上午发表声明,宣布辞职.骆家辉履任期间为中国 ...
- ZT 怎么样才算熟悉设计模式? [问题点数:40分,结帖人jiaoyun007]
http://bbs.csdn.net/topics/390448668?page=1#post-394406161 近日面试,因为个人简历里有“熟悉设计模式”这句话,面试官边侃侃发问了:什么是装饰模 ...
- Promise里捕捉错误的最佳实践
Promise里的同步部分不需要try catch new Promise((resolve, reject) => { throw new Error('error'); setTimeout ...
- Spring Framework5.0 学习(4)—— Bean的命名id和name区别
Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...