一、操作方式
1)读
注意:光标会以文本开始读取
1、以文本读取读
1、绝对路径读取
f = open('/home/pyrookie/temp.txt',mode='r',encoding='utf-8')
content = f.read()
print(content)
f.close()
2、相对路径读取
f2 = open('temp.py',mode='r',encoding='utf-8')
content = f2.read()
print(content)
f2.close()
2、以二进制读取
二进制形式 一般在打开图片,视频等文件时使用 上传下载文件时使用
# 将文件以urt-8打开 但是字符串以uncode编码的
# 所以再内部有一次转换为str
f2 = open('temp.py',mode='rb') #二进制不写编码格式
content = f2.read()
print(content)
f2.close()
3、读+ (读写)
先读取文件中的内容,再追加
f = open('log',mode='r+',encoding='utf-8')
print(f.read())
f.write('我是pyrookie')
f.close()
4、读+ (写读)
注意,写会先覆盖掉文件中的内容,读取的是覆盖之后的内容
f = open('log',mode='r+',encoding='utf-8')
f.write('aaa')
print(f.read())
f.close()
## 2)写
### 1、以文本读写入
**对于写,没有此文件就会创建文件**
**有文件,先将文件清除,再写入**
```python
f2 = open('text',mode='w',encoding='utf-8')
f2.write('写入的内容')
f2.close()
<div class="se-preview-section-delimiter"></div>
2、以二进制写入
wb写入的时候用什么编码写入就用什么打开
f2 = open('log',mode='wb')
f2.write('写入的内容'.encode('utf-8'))
f2.close()
<div class="se-preview-section-delimiter"></div>
3、读+
没什么用 只是读取不报错
f = open('log',mode='w+',encoding='utf-8')
f.write('后面加入的内容')
print(f.read())
f.close()
<div class="se-preview-section-delimiter"></div>
3)追加
1、以文本读取读
f = open('log',mode='a',encoding='utf-8')
f.write('追加的内容')
f.close()
<div class="se-preview-section-delimiter"></div>
2、以二进制读取
f = open('log',mode='ab')
f.write('追加的内容'.encode('utf-8'))
f.close()
<div class="se-preview-section-delimiter"></div>
<div class="se-preview-section-delimiter"></div>
## 4)读写
<div class="se-preview-section-delimiter"></div>
### 1、以文本读取读
<div class="se-preview-section-delimiter"></div>
### 2、以二进制读取
<div class="se-preview-section-delimiter"></div>
### 3、读+
<div class="se-preview-section-delimiter"></div>
## 5)
<div class="se-preview-section-delimiter"></div>
## 6)
<div class="se-preview-section-delimiter"></div>
# 二、功能详解
<div class="se-preview-section-delimiter"></div>
## 1)f.read(n)
功能:读取指定字符的文本信息
<div class="se-preview-section-delimiter"></div>
```python
f = open('log',mode='r+',encoding='utf-
content = f.read(3) #只读三个字符字符字符(你看到的是几个就是几个字符)
print(content)
f.close()
aaa
2)f.seek(n)
功能:将光标移动到制定的字节位置
f = open('log',mode='r+',encoding='utf-8')
f.seek(3)
# 英文 一个字母按照一个字节 一个汉字按照三个字节)(utf-8)
content = f.read(3)
print(content)
f.close()
3)f.tell()
功能:读取光标目前的位置
f = open('log',mode='r+',encoding='utf-8')
f.write('nihaoya你是不')
count = f.tell() # 读取光标位置
f.seek(count-6) # 调整光标位置(注意这以字节移动)
print(f.read())
是不12345 # 读取的内容是从写入后往前移动6个字符读取
4) f.readline()
功能:一行一行的读取
f = open('log',mode='r+',encoding='utf-8')
print(f.readline())
print(f.readline())
print(f.readline())
12345
12345
12345
5)f.readlines()
功能:每一行当成列表中的一个元素,添加到列表中
f = open('log',mode='r+',encoding='utf-8')
line = f.readlines()
print(line)
['12345\n', '12345\n', '12345\n']
可以用 str.strip() 去除字符串两边的空字符串
6)其他方法
1、f.readable()
功能:文件是否可读
f = open('log',mode='r+',encoding='utf-8')
print(f.readable())
True
2、f.truncate(n)
功能:从前往后截取几个字符
f = open('log',mode='r+',encoding='utf-8')
f.truncate(5)
# 文件中会只剩下5个字节 字节 字节
三、文件操作的几个注意事项
1)读取文件
注意读取文件时要注意不要直接读取文件的全部内容
可以使用 f.readline() 读取文件的一行或者使用f.read(n)读取文件的几个字符
2)使用 with open()as
可以使用 with open 一次打开多个文件。
with open('log',mode='r+',encoding='utf-8') as obj, open('ni',mode='w+',encoding='utf-8') as f:
print(obj.read())
3)字符串编码注意事项
1、中文的编码与解码
# str --->byte encode 编码
s = '皮皮江'
b = s.encode('utf-8')
print(b)
#byte --->str decode 解码
s1 = b.decode('utf-8')
print(s1)
b'\xe7\x9a\xae\xe7\x9a\xae\xe6\xb1\x9f'
皮皮江
2、英文编码与解码
s = 'pyrookie'
b = s.encode('utf-8')
print(b)
#byte --->str decode 解码
s1 = b.decode('gbk')
print(s1)
b'pyrookie'
pyrookie
2)使用 with open()as
with open() as :
默认是读取,编码方式为unicode
windos的文件默认都是 gbk 编码方式
可以使用 with open 一次打开多个文件。
四、文件操作
1)删除文件
import os
os.remove('文件路径/文件名')
2)重命名文件
import os
os.remove('ord文件路径/文件名','new文件路径/文件名')