[Python 从入门到放弃] 5. 文件与异常(一)
1.文件操作:
文件操作包含读/写
从文件中读取数据
向文件写入数据
Python中内置了open()方法用于文件操作 (更多关于open()BIF介绍 阅读此篇)
基本模板:
1.获取文件对象
2.文件处理:读/写/...
3.关闭文件
# .打开文件 the_file=open('f://test.txt') # f://test.txt 是绝对路径
.open(.为什么要关闭文件: 打开文件之后,会占用文件资源 在不需要使用时,应该及时关闭文件 '''
2.简单的文件读取
在python中基本的输入机制是基于行的:
(1).现在有一个文本文件:
filename:test.txt # 文件名 filePath:f:/test.txt # 路径 fileContent: # 文件内容 Man:Is this the right room for an argument?Other Man:I've told once.Man:No you haven't!Other Man:Yes I have.Man:When?Other Man:Just now.Man:No you didn't......Other Man:Now let's get one thing quite clear:I most definitely told you!Man:Oh no you did't!Other Man:Oh yes I did! '''你可以参照文件信息,在f盘创建一个'''
(2).开始读取:
the_file=open('f://test.txt') # 为什么路径是f://test.txt 不是f:/test.txt? # 因为在windows就得使用此等格式 在‘/’前加一个 # 也可以这样写: open(r'f:/test.txt') 加上r的指明读取原始字符串:后面附带的字符串是什么就用什么 不需要转义 print(the_file.readline()) the_file.close()
运行结果:
读取了文件中的第一行
readline()函数:读取一行
现在改变一下代码:
the_file=open('f://test.txt') print(the_file.readline()) print(the_file.readline()) # 使用了两个相同的语句 the_file.close()
运行结果:
结果不是输出两个相同的文件内容:
得益于文件指针,在读取该行之后,
文件指针会自动指向下一行
使用即使相同的读取语句
指针也会遵循一行行的原则指向
如何人为控制文件指针指向?
比如:当你想两次输出都输出第一行时:
可以参考seek()方法:
the_file=open('f://test.txt') print(the_file.readline()) the_file.seek(0) # 将文件指针设置在起始位置 也就是往回调 print(the_file.readline()) the_file.close()
执行结果:
(3)输出文本文件中的全部内容
方法一:使用迭代器,逐一输出:
the_file=open('f://test.txt') for line in the_file: print(line) the_file.close()
运行结果:
# 全部输出文件内容 Man:Is this the right room for an argument? Other Man:I've told once. Man:No you haven't! Other Man:Yes I have. Man:When? Other Man:Just now. Man:No you didn't ...... Other Man:Now let's get one thing quite clear:I most definitely told you! Man:Oh no you did't! Other Man:Oh yes I did!
方法二:使用read():
the_file=open('f://test.txt') print(the_file.read()) the_file.close()
read()能够将文本文件内容作为一个大字符串返回
执行结果:
Man:Is this the right room for an argument? Other Man:I've told once. Man:No you haven't! Other Man:Yes I have. Man:When? Other Man:Just now. Man:No you didn't ...... Other Man:Now let's get one thing quite clear:I most definitely told you! Man:Oh no you did't! Other Man:Oh yes I did!
当需要逐行处理时,使用方法一迭代的方法
当直接输出而不需要做其它时,使用read()方法
(4)readlines():
readline()方法是逐行读取,并返回所读取的字符串
readlines()方法是读取多行,并返回一个列表
测试一下:
the_file=open('f://test.txt') print(the_file.readlines()) the_file.close()
执行结果:
['Man:Is this the right room for an argument?\n', "Other Man:I've told once.\n", "Man:No you haven't!\n", 'Other Man:Yes I have.\n', 'Man:When?\n', 'Other Man:Just now.\n', "Man:No you didn't\n", '......\n', "Other Man:Now let's get one thing quite clear:I most definitely told you!\n", "Man:Oh no you did't!\n", 'Other Man:Oh yes I did!']
每一行都作为列表的数据项
并包含换行符’\n‘
[Python 从入门到放弃] 5. 文件与异常(一)的更多相关文章
- [Python 从入门到放弃] 6. 文件与异常(二)
本章所用test.txt文件可以在( [Python 从入门到放弃] 6. 文件与异常(一))找到并自行创建 现在有个需求,对test.txt中的文本内容进行修改: (1)将期间的‘:’改为‘ sai ...
- python入门学习:9.文件和异常
python入门学习:9.文件和异常 关键点:文件.异常 9.1 从文件中读取数据9.2 写入文件9.3 异常9.4 存储数据 9.1 从文件中读取数据 9.1.1 读取整个文件 首先创建一个pi_ ...
- [Python 从入门到放弃] 1. 列表的基本操作
''' 列表 Create By 阅后即焚 On 2018.1.29 ''' 1. 列表的定义 列表看起来好像其它编程语言中的数组,但列表具备更加强大的功能,它是Python完备的集合对象,现在,你可 ...
- Python从入门到放弃系列(Django/Flask/爬虫)
第一篇 Django从入门到放弃 第二篇 Flask 第二篇 爬虫
- python全栈开发从入门到放弃之文件处理
一.文件处理流程 1.打开文件,得到文件句柄并赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 事例文件内容 [一棵开花的树] 如何让你遇见我 在我最美丽的时刻 为这 我已在佛前求了五百年 求 ...
- Python从入门到放弃
计算机基础 01 计算机基础之编程 02 计算机组成原理 03 计算机操作系统 04 编程语言分类 Python解释器 05 Python和Python解释器 06 执行Python程序的两种方式 0 ...
- [Python 从入门到放弃] 3. BIF(内建函数)
BIF (built-in functions) Python中提供了70多个内建函数,具备大量的现成功能. BIF不需要专门导入,可以直接使用,拿来就用 1.print() # 在屏幕上打印输出 如 ...
- python从入门到放弃之进程
在理解进程之前我们先了解一下什么是进程的概念吧 以下就是我总结的一些基本的进程概念 进程就是正在运行的程序,它是操作系统中,资源分配的最小单位(通俗易懂点也就是电脑给程序分配的一定内存操作空间).资源 ...
- python从入门到放弃之Tensorflow(一)
Tensorflow使用错误集锦: 错误1 : FutureWarning: Conversion of the second argument of issubdtype from ‘float’ ...
随机推荐
- 使用 WLST 和节点管理器来管理服务器
使用节点管理器启动计算机上的服务器 WLST 可以连接至在任何计算机上运行的节点管理器,并能够在此计算机上启动一个或多个 WebLogic Server 实例.要通过此技术使用 WLST 和节点管理器 ...
- 属性动画和Activity、Fragment过渡动画等
主题是关于动画的,但是不是什么动画的内容都包括.先泛泛的介绍一下,然后详细的介绍一下翻代码找见的一个好玩的动画的使用.以下的内容包括Android 3和Android 3.1等引入的API,在使用中请 ...
- Job Interview: Why Only 3 Questions Really Matter
Even for the most fearless amongst us, job interviews can be nerve wracking. In order to give us the ...
- 利用HttpURLConnection发送post请求上传多个文件
本文要用java.net.HttpURLConnection来实现多个文件上传 1. 研究 form 表单到底封装了什么样的信息发送到servlet. 假如我参数写的内容是hello word,然后二 ...
- 《mysql必知必会》学习_第六章_20180730_欢
第六章<过滤数据> P35 1. select prod_name,prod_price from products where prod_price=2.5; 2.select prod ...
- Python自动化开发 -进程、线程和协程(二)
本节内容 一.线程进程介绍 二. 线程 1.线程基本使用 (Threading) 2.线程锁(Lock.RLock) 3.信号量(Semaphore) 4.事件(event) 5.条件(Conditi ...
- Android-Version Compatibility Issues (Gradle 2.14.1 requires Android Gradle plugin 2.1.3 (or newer)) but project is using
当AndroidStudio加载工程Project的时候,出现以上错误❌,千万不要点击,否则就是更多其他的错误: 解决方案: 1.认真翻译错误: 2.分析问题发生的原因,然后看到了 ..... ...
- Windows核心编程:第12章 纤程
Github https://github.com/gongluck/Windows-Core-Program.git //第12章 纤程.cpp: 定义应用程序的入口点. // #include & ...
- Opencv3.4:显示一张图片
Github https://github.com/gongluck/Opencv3.4-study.git #include "opencv2/opencv.hpp" using ...
- c# MVC Action 如何知道 发送方给你的 Json 数据的格式内容是什么
public class DemoModel { public string Name { get; set; } public int Age { get; set; } } [HttpPost] ...