我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所周知,应用程序是无法直接操作硬件的,这就用到了操作系统。操作系统把复杂的硬件操作封装成简单的接口给用户/应用程序使用,其中文件就是操作系统提供给应用程序来操作硬盘虚拟概念,用户或应用程序通过操作文件,可以将自己的数据永久保存下来。
# 1. 打开文件,得到文件句柄并赋值给一个变量
f = open('target_file.txt', mode='r', encoding='utf-8')
# 2. 通过句柄对文件进行操作
content = f.read()
# 3.输出读取到的结果
print(content)
# 4. 关闭文件
f.close()
>>> 文件中的内容:
>>> python
>>> java
# 1. with关键字引导上下文方式打开文件,并将句柄赋予别名f
with open('target_file.txt', mode='r', encoding='utf-8') as f:
# 2. 通过句柄对文件进行操作
content = f.read()
# 3.输出读取到的结果,用with上下文方式对文件进行操作会在结束操作时默认关闭文件,不必要主动close()
print(content)
Character | Meaning |
---|---|
‘r’ | open for reading (default) |
‘w’ | open for writing, truncating the file first |
‘a’ | open for writing, appending to the end of the file if it exists |
‘b’ | binary mode |
‘t’ | text mode (default) |
‘+’ | open a disk file for updating (reading and writing) |
‘U’ | universal newline mode (for backwards compatibility; should not be used in new code) |
# 此处target_file.txt文件中原始内容为:人生苦短,我用python
# ①r——只读,文件不存在则报错
with open('target_file.txt', mode='r', encoding='utf-8') as f:
content = f.read()
print(content)
>>> 人生苦短,我用python
# ②rb——以bytes类型读
with open('target_file.txt', mode='rb') as f:
content = f.read()
print(content)
>>> b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8python'
# ③r+——读写,文件不存在不会创建,写则会在指针后添加内容
with open('target_file.txt', mode='r+',encoding='utf-8') as f:
content = f.read()
f.write('\n新添加:Life is short , I use python')
print(content)
>>> 人生苦短,我用python
# target_file.txt中的内容为:
>>> 人生苦短,我用python
>>> 新添加:Life is short , I use python
# ⑤r+b——以bytes类型读写
with open('target_file.txt', mode='r+b') as f:
content = f.read()
f.write('\n新添加:Life is short , I use python'.encode('utf-8'))
print(content)
>>> b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8python'
# target_file.txt中的内容为:
>>> 人生苦短,我用python
>>> 新添加:Life is short , I use python
# 此处target_file.txt文件中原始内容为空,需要向其中写入:人生苦短,我用python
# ①w——只写,文件不存在则创建,文件存在则清空再写
with open('target_file.txt', mode='w', encoding='utf-8') as f:
content = f.write('人生苦短,我用python')
# target_file.txt中的内容为:
>>> 人生苦短,我用python
# ②x——只写,文件不存在则创建,文件存在则报错
with open('target_file.txt', mode='x', encoding='utf-8') as f:
content = f.write('人生苦短,我用python')
# target_file.txt存在:
>>> Traceback (most recent call last):
>>> File "D:/python_fullstack_s9/day8/practice.py", line 94, in
>>> with open('target_file.txt', mode='x', encoding='utf-8') as f:
>>> FileExistsError: [Errno 17] File exists: 'target_file.txt'
# target_file.txt不存在:
>>> 人生苦短,我用python
# # ③wb——以bytes类型写
with open('target_file.txt', mode='wb') as f:
content = f.write('人生苦短,我用python'.encode('utf-8'))
>>> 人生苦短,我用python
# # ④w+——写读,文件不存在则创建,写会覆盖之前的内容
with open('target_file.txt', mode='w+') as f:
content = f.write('hello,world')
date = f.read()
print(date)
>>>
# target_file.txt中的内容为:
>>> hello,world
# # ⑤w+b——以bytes类型写读
with open('target_file.txt', mode='w+b') as f:
content = f.write('hello,world'.encode('utf-8'))
date = f.read()
print(date)
>>> b''
# target_file.txt中的内容为:
>>> hello,world
# 此处target_file.txt文件中原始内容为:人生苦短,我用python,需要在后面添加'谁用谁知道'内容
# ①a——追加,文件不存在则创建,文件存在则追加
with open('target_file.txt', mode='a', encoding='utf-8') as f:
content = f.write('谁用谁知道')
target_file.txt中的内容为:
>>> 人生苦短,我用python谁用谁知道
# ②ab——以bytes类型追加
with open('target_file.txt', mode='ab') as f:
content = f.write('谁用谁知道'.encode('utf-8'))
>>> 人生苦短,我用python谁用谁知道
# ③a+——可读可写,文件不存在则创建,写则追加
with open('target_file.txt', mode='a+',encoding='utf-8') as f:
content = f.write('谁用谁知道')
f.seek(0)
date = f.read()
print(date)
>>> 人生苦短,我用python谁用谁知道
# ④a+b——以bytes类型可读可写
with open('target_file.txt', mode='a+b') as f:
content = f.write('谁用谁知道'.encode('utf-8'))
f.seek(0)
date = f.read()
print(date)
>>> b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8python\xe8\xb0\x81\xe7\x94\xa8\xe8\xb0\x81\xe7\x9f\xa5\xe9\x81\x93'
# target_file.txt中的内容为:
>>> 人生苦短,我用python谁用谁知道
python没有提供直接修改文件的函数,只能新建文件,将源文件的内容修改完成后写入新文件中,再把原文件删除,新文件重命名
import os
with open('源文件', encoding='utf-8') as f, open('源文件.bak', mode='w', encoding='utf-8') as f_w:
for i in f:
if '源文件' in i:
i = i.replace('源文件', '更改后的源文件')
f_w.write(i)
os.remove('源文件')
os.rename('源文件.bak','源文件')
seek有三种移动方式0,1,2,其中1和2必须在b模式下进行,但无论哪种模式,都是以bytes为单位移动的
tell对于英文字符就是占一个,中文字符占三个,参数表示的是字节数区分与read()的不同.
truncate是截断文件,所以文件的打开方式必须可写,但是不能用w或w+等方式打开,因为那样直接清空文件了,所以truncate要在r+或a或a+等模式下测试效果
with open('target_file.txt', mode='r',encoding='utf-8') as f:
for i in f:
print(i)
>>> 人生苦短,我用python
>>> 谁用谁知道