简析python 文件操作
一、打开并读文件
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可以对比来记。