首页 > 编程技术 > python

python基础之文件处理知识总结

发布时间:2021-5-23 22:02

一、open()方法

python open()方法用于打开一个文件,并返回文件对象,在对文件处理的过程中都需要用到这个函数,如果文件无法打开,会抛出OSError。

注意:使用open()方法的时候一定到保证关闭文件对象,文件里面的内容才会被保存,关闭文件需要调用close()方法。

open()方法常用的形式是接收两个参数:文件名(file)和模式(mode)

基本语法

open(file,mode='r')

完整的语法:

open(file,mode='r',buffering=1,encoding=None,errors=None,newline=None,closefd=True)

参数说明:

mode表示文件打开模式,有几种模式呢?参考如下:

例1:以w模式打开文件

f = open('myfile.txt', 'w')
f.write('hello,world!')
f.close()
 
##输出结果
在当前路径下成一个myfile.txt文件,并且把'hello world'写到该文件
 
myfile.txt内容如下:
hello,world!

例2: 以a模式打开文件

f=open('myfile.txt','a')
f.write('\ngood lucky')
f.close()
 
##输出结果:
会在文件末尾追加内容,不会覆盖原来的内容
 
myfile.txt内容如下:
hello,world!
good,lucky!

例3: 再以w模式打开文件,会把原来内容覆盖掉

f = open('myfile.txt', 'w')
f.write('welcome!')
f.close()
 
##输出内容:
 
myfile.txt内容如下:
welcome!

例4: 以r的模式读文件

f = open('myfile.txt', 'r') #以r模式读文件,再往里面write会报错
f.write('\nhello!')
f.close()
 
## 输出内容:
    f.write('\nhello!')
io.UnsupportedOperation: not writable

例5: 以r+的模式读文件

f = open('myfile.txt', 'r+') 
f.write('\nhello!')
f.close()
 
##输出结果
r+:打开一个文件用于读写,文件指针将会放在文件的开头
 
myfile.txt内容:
1--------------> 此处表示空行
2hello!

例6: 以w+模式写文件

f = open('myfile.txt', 'w+')
f.write('love!')
f.close()
 
##输出结果:
如果该文件已经存在,则打开已经存在文件,并且从头开始编辑,即原有的内容会被删除。如果该文件不存在,则创建新文件。
 
myfile.txt内容:
love!

二、read()方法

读取文件中的所有内容,读取之后光标移动到文件末尾。 必须以r或r+模式,才可以使用read()。

例7: 以w+模式写文件,再以r+模式来读取文件内容

f = open('myfile.txt', 'w+')
f.write('hello,world!\ngood,lucky!!')
f.close()
 
##输出结果:
w+ 如果该文件已经存在,则打开已经存在文件,并且从头开始编辑,即原有的内容会被删除。如果该文件不存在,则创建新文件。
 
myfile.txt内容:
hello,world!
good,lucky!!

f = open('myfile.txt', 'r+') #
print(f.read())
 
##输出结果:
hello,world!
good,lucky!!

例8:以r+模式写文件,再来读取文件

f = open('myfile.txt', 'r+') #以r+的模式,会将文件指针放在开头,然后将指定字符替换掉文件中原来的字符  
f.write('\nwelcom')
print(f.read())
 
##输出结果:
rld!
good,lucky!!
 
#myfile.txt内容如下:
1----------->空行
2welcomrld!
3good,lucky!!

三、readlines()方法

readlines()一行行读文件

例9:

f = open('myfile.txt', 'r+')
print(f.readline())
print(f.readline())
print(f.readline())
 
##输出结果
 
 
welcomrld!
 
good,lucky!!

readlines()

一行行读取文件内容,然后存放在列表中,可以读取所有行的内容,每行的内容都作为列表中的一个元素存在列表里,并且返回一个列表。 这个列表可以使用for..in 结构进行处理。 如果碰到EOF结束符,则返回空字符。

例10:

f = open('myfile.txt', 'r')
print(f.readlines())
 
##输出内容:
['\n', 'welcomrld!\n', 'good,lucky!!']
 
 
 
f = open('myfile.txt', 'r')
for i in f.readlines():
    i = i.strip()   # 去掉空格,如\n换行符
    print(i)
 
##输出内容:
----------->空行
welcomrld!
good,lucky!!

四、seek()方法

seek() 用于移动文件读取指针到指定位置

语法如下:f.seek(offset,[,whence])
offset--开始的偏移量,也就是代表需要移动偏移的字节数,如果是负数,表示从倒数第几位开始
whence--可选参数,默认是0。给offset定义一个参数,表示从哪个位置开始偏移;0代表从文件开头算起;1代表从当前位置开始算起;2代表从文件末尾算起。 

如果操作成功,则返回新的文件位置;如果操作失败,返回-1

例11:

f = open('workfile.txt', 'wb+')
print(f.write(b'0123456789abcde'))
f.seek(5)
print(f.read(1))
f.seek(-3,2)
print(f.read(1))
 
##输出结果:
15
b'5'
b'c'
 
workfile.txt内容如下:
0123456789abcde

例12:

f = open('myfile.txt', 'r')
print('filename is :',f.name)
line=f.readline().strip()           ##去掉\n
print('第一次读取的数据是%s' % (line))
f.seek(0, 0)                        ##第一个0表示偏移量为0,第二个0表示从文件头开始偏移
line=f.readline().strip()
print('第二次读取的数据是%s' % (line))
 
##输出内容:
filename is : myfile.txt
第一次读取的数据是good,lucky!!
第二次读取的数据是good,lucky!!
 
myfile.txt内容如下:
good,lucky!!

##去掉 f.seek(0, 0),运行结果如下:
f = open('myfile.txt', 'r')
print('filename is :',f.name)
line=f.readline().strip()
print('第一次读取的数据是%s' % (line))
line=f.readline().strip()
print('第二次读取的数据是%s' % (line))
 
##运行结果如下:
filename is : myfile.txt
第一次读取的数据是good,lucky!!
第二次读取的数据是

五、tell()函数 

返回文件的当前位置

参考: https://www.runoob.com/python/file-tell.html

f = open('myfile.txt', 'r+')
print('filename is :', f.name)
line=f.readline()
print('读取的数据是%s' % (line))
 
 
line1=f.readline()
print('读取的数据是%s' % (line1))
pos=f.tell()
print('current position is %d:' % (pos) )
f.close()
 
##输出结果
filename is : myfile.txt
读取的数据是good,lucky!!
读取的数据是
current position is 12:
 
myfile.txt内容如下:
good,lucky!!
 
 
 
 
##输出结果
filename is : myfile.txt
读取的数据是good,lucky!!
 
读取的数据是g
current position is 15:
 
myfile.txt内容如下:
good,lucky!!
g

到此这篇关于python基础之文件处理知识总结的文章就介绍到这了,更多相关python文件处理内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

标签:[!--infotagslink--]

您可能感兴趣的文章: