读写文本数据
读数据
with open('file_dir','r') as f:
data = f.read()
写数据
with open('file_dir','w') as f:
data = f.write('hello')
以上是最基础的文件读取形式,我们要关注的是 读的方式 和 写的方式。
符号 | 功能 |
---|---|
w | 只写,文本内容将会清空。不存在则创建 |
w+ | 读写,文本内容将会清空。不存在则创建 |
r | 只读,文本内容将会清空。不存在则报错 |
r+ | 读写,文本内容将会清空。不存在则报错,写操作会覆盖 |
a | 只写,对文本内容进行追加。不可读不存在则创建 |
a+ | 读写,对文本内容进行追加。可读不存在则创建 |
x | 写入,文件不存在时才能写入,否则报错 |
b | 与以上符号相搭配,以二进制方式操作 |
t | txt文档处理 windows下换行替换。如 rt —— ‘\r\n’ - ‘\n’ wt – ‘\n’ - ‘\r\n’ |
指定编码方式
with open('file_dir','r',encoding='latin-1') as f:
打印输出到文件中
with open('file_dir','r') as f:
print('hello !',file=f)
文件必须是以文本模式打开,如果是二进制的话,会出错。
指定分隔符及终止符打印
>>> print('Hello',1,2,3)
Hello 1 2 3
>>> print('Hello',1,2,3,sep=',')
Hello,1,2,3
>>> print('Hello',1,2,3,sep=',',end='')
Hello,1,2,3>>>
读写字节数据
例如读写二进制文件,像图片,声音文件等。
对于二进制文件的读写可以用 rb 和 wb 来进行。
# 读取 图片1.png
with open('1.png','rb') as f:
data = f.read()
#将 图片1.png数据写入 文件2.png
with open('2.png','wb') as f:
f.write(data)
在读取二进制数据的时候,返回的数据是字节字符串格式而不是文本字符串。也就是说写入二进制文件的时候也应该保持字节字符串格式,否则会出现一些不必要的错误。对于二进制文件读写文本数据的问题可以使用编码解码操作过渡
>>> import io
>>> for s in 'py':
>>> print(s)
p
y
>>> for s in b'py':
>>> print(s)
112
121
字符串的 I/O 操作
使用操作类文件对象的程序来操作文本或二进制字符串。
操作字符串数据
>>> s = io.StringIO()
>>> s.write("hello world")
11
>>> print('i want to say:')
14
#获取io中的s对象的值,io不是文件,所以获取后就没有值了
>>> s.getvalue()
'hello worldi want to say:'
>>> s = io.StringIO('hello everyone')
>>> s.read(4)
'hell'
>>> s.read()
'o everyone'
操作二进制数据
s = io.BytesIO()
s.write(b'hello')
...
读写压缩文件
读写一个 gzip 或 bz2 格式的压缩文件
import gzip,bz2
with gzip.open('somefile.gz', 'rt') as f:
...
with bz2.open('somefile.gz', 'rt') as f:
...
读取二进制数据到可变缓冲区中
为了读取数据到一个可变数组中,使用文件对象的 readinto() 方法。比如:
import os.path
def read_into_buffer(filename):
buf = bytearray(os.path.getsize(filename))
with open(filename, 'rb') as f:
f.readinto(buf)
return buf
>>> # Write a sample file
>>> with open('sample.bin', 'wb') as f:
... f.write(b'Hello World')
...
>>> buf = read_into_buffer('sample.bin')
>>> buf
bytearray(b'Hello World')
>>> buf[0:5] = b'Hallo'
>>> buf
bytearray(b'Hallo World')
>>> with open('newsample.bin', 'wb') as f:
... f.write(buf)
...
11
文件路径名的操作
方法 | 功能 | 结果 |
---|---|---|
path_dir = /1/2/3/4.txt | ||
os.path.basename(path_dir) | 获取一个路径最底层名 | 4.txt |
os.path.dirname(path_dir) | 获取所在文件夹名 | /1/2/3 |
os.path.join | 合并路径 | |
os.path.expanduser(path_dir) | 展开文件目录 | |
os.path.splitext(path_dir) | 分隔后缀名 | |
os.path.exists(path_dir) | 测试文件或目录是否存在 | |
os.path.isfile(path_dir) | 判断是否为文件 | |
os.path.isdir(path_dir) | 判断是否为目录 | |
os.path.islink(path_dir) | 判断是否为链接 | |
os.path.realpath(path_dir) | 获取链接指向路径 | |
os.listdir(path_dir) | 获取目录中的文件列表 |
创建临时文件和文件夹
在程序执行时创建一个临时文件或目录,使用完后自动销毁
from tempfile import TemporaryFile
with TemporaryFile('w+t') as f:
f.write('Hello World\n')
f.write('Testing\n')
f.seek(0)
data = f.read()
序列化python对象
将一个python对象序列化为一个字节流,以便网络传输或者是保存到文件/数据库中。
* 使用 pickle 模块对数据进行序列化 *
#将一个对象保存到文件中
import pickle
data = [1,2,3,4]
f = open('1.txt','wb')
pickle.dump(data,f)
f.close()
#恢复一个对象
f = open('1.txt','rb')
data = pickle.load(f)
f.close()