python(day021——文件1)

1.文件操作:读、写、追加

  • 读:open(参数1:文件路径,参数2:打开模式,参数3:文件编码)

文件路径:

相对路径:在E盘下a.txt

绝对路径:e:\\test\\a.txt

fp.close()

fp=open("e:\\b.txt",encoding="gbk")
content=fp.read()
fp.close()
print(content)

 

2.读的时候处理文件状态:

import os

if os.path.exists("e:\\axxxxx.txt"):
    fp=open("e:\\axxxxx.txt",encoding="utf-8")
    content = fp.read()
    fp.close()
    print(content)
try:
    fp=open("e:\\axxxxx.txt",encoding="utf-8")
    content = fp.read()
    fp.close()
    print(content)
except IOError:
    print("此文件的读操作遇到io错误")

3.操作系统有资源句柄:一个指针指向了一个文件,就可以操作文件了,例如读写。

句柄在操作系统资源是有限的,65535个。如果所有句柄都被打开,且不释放,句柄泄露。

4.读行和读全部

>>> fp=open("e:\\hello.txt","r",encoding="gbk")
>>> fp.readline(2)
'白日'
>>> fp.readline(2)
'依山'
>>> fp.readline(2)
'尽\n'
>>> fp.readline(2)
'黄河'
>>> fp.close()

 5.遍历行

6.定位位置:fp.seek(offset[, whence])

  • offset -- 开始的偏移量,也就是代表需要移动偏移的字节数

  • whence:可选,默认值为 0。给offset参数一个定义,表示要从哪个位置开始偏移;0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。

  • 返回值:如果操作成功,则返回新的文件位置,如果操作失败,则函数返回 -1。

  • gbk存中文,是两个字符;utf-8存中文,是三个字符。

  • utf-8文件,默认有bom。

>>> fp=open("e:\\a.txt","r",encoding="gbk")
>>> fp.readline()
'第一行\n'
>>>
>>> fp.readline()
'第二行\n'
>>> fp.seek(0,0)
0
>>> fp.readline()
'第一行\n'
>>> fp.seek(2,0)  #第一个参数表示第二个字节,第二个参数表示从第几行
2
>>> fp.readline()
'一行\n'
>>> fp.close()
>>> fp=open("e:\\a.txt","r",encoding="utf-8")
>>> fp.readline()
'\ufeff第一行\n'
>>> fp.readline()
'第二行\n'
>>> fp.readline()
'第三行\n'
>>> fp.seek(2,0)
2
>>> fp.readline()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Administrator.DESKTOP-LFNQE7C\AppData\Local\Programs\Python\Python37\li
b\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 0: invalid start by
te
3
>>> fp.readline()
'第一行\n'
>>> fp.seek(6,0)
6
>>> fp.readline()
'一行\n'

练习:

>>> fp=open("e:\\a.txt")  #fp=open("e:\\a.txt","r",encoding="gbk")
>>> #默认使用gbk编码读取文件,并且使用读模式
...
>>> content=fp.read()
>>>
>>> print(content)
第一行
第二行
第三行

第五行
>>> fp.close()
#以上全读方式,如果文件很大,则把内存占满


>>> fp.readlines()
['第一行\n', '第二行\n', '第三行\n', '\n', '第五行']
>>> fp.close()



>>> fp=open("e:\\a.txt","r",encoding="gbk")
>>> for line in fp:
...    print(line)
...
第一行

第二行

第三行



第五行
>>> fp.close()
#以上,读大文件,应该按行读


>>> fp=open("e:\\a.txt","r",encoding="gbk")
>>> fp.readline()
'第一行\n'
>>> fp.readline()
'第二行\n'
>>> fp.readline().strip()
'第三行'
>>> fp.readline().strip()
''
>>> fp.readline().strip()
'第五行'
>>> fp.readline().strip()
''
>>> fp.close()

7.写:

"w":清空写

w:如果文件存在,则清空文件内容开始写;如果文件不存在,则新建文件后开始写内容。

写入时文件有缓存机制,可使用flush()方法,把缓存的命令执行。

"a":追加写

>>> fp=open("e:\\hello.txt","w",encoding="gbk")
>>> fp.write("白日依山尽\n")
6
>>> fp.write("黄河日还了\n")
6
>>> fp.flush()
>>> fp.close()
>>> fp=open("e:\\hello.txt","a",encoding="gbk")
>>> fp.write("欲穷千里目\n")
6
>>> fp.write("更上一层楼\n")
6
>>> fp.flush()
>>> fp.close()

8.写、读:

"w+"清空写,可以读

"r+"写,可以读

#1. r+新写入的内容会覆盖原文件中的内容,写入几个字符,则覆盖几个字符
#2. r+会从文件开头开始进行文件读写,所以每次写入时,都会从一开始(第一行)进行。如果先读后写,则会写在最后。
#3. \n换行符相当于两个字符
#4. r+写完后,文件指针会停留在新写入的字符后面,所以在read时,只read新写入字符后面的那些内容

>>> fp=open("e:\\hello.txt","w+",encoding="gbk")
>>> fp.write("白日依山尽\n")
6
>>> fp.write("黄河入海流\n")
6
>>> fp.seek(0,0)
0
>>> fp.read()
'白日依山尽\n黄河入海流\n'
>>> fp.close()

9.tell() 方法返回文件的当前位置,即文件指针当前位置。

>>> fp=open("e:\\hello.txt","r",encoding="gbk")
>>> fp.readline(2)
'he'
>>> fp.tell()
2
>>> fp.seek(0,0)
0
>>> fp.tell()
0
>>> fp.close()

10.二进制读:rb、wb

一般用于对图片的复制等操作。

>>> fp=open("e:\\1.jpg","rb")
>>> content=fp.read()
>>> fp1=open("e:\\new_1.jpg","wb")
>>> fp1.write(content)
28436
>>> fp.close()
>>> fp1.close()

11.os包

>>> import os
>>>
>>> os.name
'nt'
>>> os.linesep
'\r\n'
>>> os.pathsep
';'
>>> os.getcwd()  #查看当前所在目录
'E:\\'
>>> os.chdir("d:\\test")  #改变当前所在目录
>>> os.getcwd()
'd:\\test'
>>> os.listdir()
['1.jpg', 'a.txt', 'b.txt', 'c.txt', 'new_1.jpg']
>>> os.path.isfile("d:\\1.jpg")
False
>>> os.path.isfile("d:\\test\\1.jpg") #判断是不是文件
True
>>> os.path.isdir("d:\\test")  #判断是不是目录
True
>>> os.path.isdir("d:\\test\\1.jpg")
False
>>> os.path.exists("d:\\test\\1.jpg")  #判断路径是否存在
True
>>> os.path.exists("d:\\test\\2.jpg")
False
>>> os.path.split("d:\\test\\1.jpg")  #返回路径名和文件名
('d:\\test', '1.jpg')
>>> os.path.dirname("d:\\test\\1.jpg")  #返回路径名
'd:\\test'
>>> os.path.basename("d:\\test\\1.jpg")  #返回文件名
'1.jpg'
>>> os.makedirs("d:\\test\\test1\\test2")  #新建目录
>>> os.removedirs("d:\\test\\test1\\test2")  #删除目录
>>> os.remove("d:\\new_1.jpg")  #删除文件,文件不存在报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'd:\\new_1.jpg'
>>> os.remove("d:\\test\\new_1.jpg")  #删除文件
>>> os.path.splitext("d:\\test\\1.jpg")  #返回文件名和后缀名
('d:\\test\\1', '.jpg')

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值