网站首页 > 技术文章 正文
一、打开并读文件
1、file = open('打开文件的路径','打开文件的权限')#打开文件并赋值给file
#默认权限为r及读权限
str = read(num)读文件并放到字符串变量中,其中num表示要读取的字节数,
#默认read函数不加参数是全读
str = file.read()
file.close()#关闭文件,线程回收
2、下面举个列子(程序用华丽的分割线隔开):
首先我们创建一个test.txt文件随便写入下面内容(Apologize的歌词)
Apologize
I'm holding on your rope,
Got me ten feet off the ground
I'm hearin what you say but I just can't make a sound
You tell me that you need me
Then you go and cut me down, but wait
You tell me that you're sorry
Didn't think I'd turn around, and say...
------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------
file = open(r'/root/python-learn/python1-7/test.txt')
#r,表示防止转义,也可以用\来防止转义
str = file.read()
print(str)
file.close()
------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------
输出结果:
3、这里我们要说明下读写指针
#文件读写指针,当读完一次后,str1将接着str后读,但是str后面会自动添加\n
------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------
file = open(r'/root/python-learn/python1-7/test.txt')
str = file.read(10)
str1 = file.read(10)
print(str)
print(str1)
file.close()
------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------
输出结果:
4、为了修改读写指针我们使用到seek()函数
语法:fileObject.seek(offset[,whence])
offset:偏移量
whence:从哪里
0 表示从头开始计算
1 表示从当前稳只计算
2 表示以文件末尾为远点进行计算
需要注意的是,如果文件以a或a+的模式打开,每次进行写操作时,文件操作标记会自动返回到
文件末尾
file.seek(0,0)回到文件开头
file.seek(-1,2)从末尾向前偏移一个,尝试后发现最好用rb的权限读,rb以二进制方式读
------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------
file = open(r'/root/python-learn/python1-7/test.txt','rb')
file.seek(-20,2)
str1 = file.read(10)
print(str1)
file.close()
------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------
输入结果:(能不使用图片就不粘图了提高效率)
[root@vipuser200 python1-7]# python3 file.py
b'around, an'
5、读取行使用函数readline()
str = readline() 读取一行
------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------
file = open(r'/root/python-learn/python1-7/test.txt')
str1 = file.readline()
print(str1)
str2 = file.readline()
print(str2)
str3 = file.readline()
print(str3)
str4 = file.readline()
print(str4)
str5 = fisle.readline()
print(str5)
file.close()
------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------
输出结果:(原文件里面只有一个空行,但程序自带换行符所以会感觉空行比较多)
[root@vipuser200 python1-7]# python3 file1.py
Apologize
I'm holding on your rope,
Got me ten feet off the ground
I'm hearin what you say but I just can't make a sound
6、strlist = readlines() 读取整个文件到字符串列表
字符串列表:['abc','bcd']里面所有元素必须是字符串,可以把文件中的内容
一次性读到字符串列表中。
怎么去掉换行符呢
------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------
file = open(r'/root/python-learn/python1-7/test.txt')
strlist = file.readlines()
print(strlist)
for var in strlist:#也可以用file.strip()去掉不可见字符
var = var[:-1]#切片首先你得确定你最后一个换行符是单个字符
print(var)
file.close()
------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------
输出结果:
[root@vipuser200 python1-7]# python3 file2.py
['Apologize\n', '\n', "I'm holding on your rope,\n", #后面太长不粘了
I'm holding on your rope,
Got me ten feet off the ground
I'm hearin what you say but I just can't make a sound
You tell me that you need me
Then you go and cut me down, but wait
You tell me that you're sorry
Didn't think I'd turn around, and say...
二、文件写操作
1、file.write('str') #在文件中写入字符串,当你使用写模式打开文件的时候,会将
文件里面的内容清空。
首先我们创建一个test1.txt测试文件在里面写入
Aplologize
编写程序
------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------
file = open(r'/root/python-learn/python1-7/test1.txt','w')
file.write('---------华丽的分割线-------------')
print(file)
file.close()
------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------
执行结果:
[root@vipuser200 python1-7]# python3 file3.py
<_io.TextIOWrapper name='/root/python-learn/python1-7/test1.txt' mode='w'
encoding='UTF-8'>
打开test1.txt
---------华丽的分割线-------------
python中的写操作不会默认加换行符(需要自己手动添加)
python中的写不会覆盖原先的内容,只有我们重新打开文件再次使用w模式时候,文件
内容才会覆盖
------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------
from time import sleep
file = open(r'/root/python-learn/python1-7/test1.txt','w')
file.write('---------华丽的分割线-------------')
file.write('********华丽的星号*********')
file.flush() #强制写入,不需要等到文件关闭
print(file)
sleep(5) #等待5秒
file.close()
------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------
输出结果:
[root@vipuser200 python1-7]# python3 file3.py
<_io.TextIOWrapper name='/root/python-learn/python1-7/test1.txt' mode='w'
encoding='UTF-8'>
打开test1.txt文件显示如下(里面使用了sleep()函数)
---------华丽的分割线-------------********华丽的星号*********
2、file.writelines()在文件中写入字符串元组或者是字符串列表
程序如下:
------------↓↓↓↓↓↓这是程序↓↓↓↓↓↓↓--------------
file = open(r'/root/python-learn/python1-7/test1.txt','w')
strlist = ['aaa','bbb']
file.writelines(strlist)
file.close()
------------↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑--------------
输出结果:
打开文件test1.txt
aaabbb
如果我们想让输入的字符串换行需要手动加入换行符可以写成如下所示:
strlist = ['aaa\n','bbb\n']
readlines和writelines可以对比来记。
猜你喜欢
- 2025-05-02 python执行.sql语法和文件(python oracle sql语句跟参数)
- 2025-05-02 python unittest 基本用法(python中testcase)
- 2025-05-02 免费定时运行Python程序并存储输出文档的服务推荐
- 2025-05-02 20 天学 Python 文件操作:Day 1 从 open() 开始
- 2025-05-02 一文掌握Python找到文件操作(python找到文件夹下指定文件)
- 2025-05-02 python 文件操作(python 文件操作模块)
- 2025-05-02 pdb,让python文件在linux中跑起来
- 2025-05-02 IDEA中配置Python环境并运行(idea 运行python)
- 2025-05-02 14《Python 办公自动化教程》os 模块操作文件与文件夹
- 2025-05-02 Python自动化办公自学笔记(八)文件操作
- 261℃Python短文,Python中的嵌套条件语句(六)
- 261℃python笔记:for循环嵌套。end=""的作用,图形打印
- 260℃PythonNet:实现Python与.Net代码相互调用!
- 255℃Python实现字符串小写转大写并写入文件
- 254℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 110℃原来2025是完美的平方年,一起探索六种平方的算吧
- 94℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 87℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)