当前文件和pi_digits.txt在同一层级目录结构下:
with open(r"pi_digits.txt") as f:
contents = f.read()
print(contents)
打开文件的方式:
r, w, a
r+, w+,a+
rb, wb, ab
r+b, w+b, a+b
r 模式:只读
f = open('pi_digits.txt',mode='r',encoding='utf-8')
contents = f.read()
print(contents)
f.close()
# 输出结果 圆周率:3.1415926535
rb 模式:只读字节
f = open('pi_digits.txt',mode='rb')
contents = f.read()
print(contents)
f.close()
# 输出结果 b'\xe5\x9c\x86\xe5\x91\xa8\xe7\x8e\x87:3.1415926535'
相对路径
相对路径就是以当前文件为基准,进行一级级目录指向被引用的的资源文件。
绝对路径
绝对路径就是文件的真正存在的路径,是指从硬盘的根目录开始,进行一级级目录指向文件。
以下是常用的表示当前目录和当前目录的父级目录的标识符
└── mypackage
├── subpackage_1
│ ├── test11.py
│ └── test12.py
└── subpackage_2
├── test21.py
└── test22.py
以test11.py为主文件,. / 表示subpackage_1;. . / 表示mypackage。
补充__init__.py的作用
(1)标识该目录是一个python的模块包(module package)
如果使用python相关的IDE进行开发,如果目录中存在该文件,该目录就会被识别为 module package
(2)简化模块导入操作
└── mypackage
├── subpackage_1
│ ├── test11.py
│ └── test12.py
├── subpackage_2
│ ├── test21.py
│ └── test22.py
└── subpackage_3
├── test31.py
└── test32.py
└── mypackage
├── __init__.py
├── subpackage_1
│ ├── test11.py
│ └── test12.py
├── subpackage_2
│ ├── test21.py
│ └── test22.py
└── subpackage_3
├── test31.py
└── test32.py
print("You have imported mypackage")
>>> import mypackage
You have imported mypackage
在mypackage/__init__.py 文件中写入需要导入的模块
from mypackage import subpackage_1,subpackage_2,subpackage_3
等价于
__all__ = ['subpackage_1','subpackage_2','subpackage_3']
└── mypackage
├── __init__.py
├── subpackage_1
│ ├── __init__.py
│ ├── test11.py
│ └── test12.py
├── subpackage_2
│ ├── test21.py
│ └── test22.py
└── subpackage_3
├── test31.py
└── test32.py
在subpackage_1/__init__.py 文件中写入需要导入的模块
from mypackage.subpackage_1 import test11, test12
等价于
__all__ = ['test11','test12']
read(): 是将文件中所有的内容都读取
with open(r'star.txt',mode='r',encoding='utf-8') as f:
contents = f.read()
print(contents)
# 输出结果:
高圆圆
贾静雯
周芷若
赵敏
with open(r'star.txt',mode='r',encoding='utf-8') as f:
contents = f.read(3)
contents2 = f.read()
print(contents)
print(contents2)
# 输出结果:
高圆圆
贾静雯
周芷若
赵敏
with open(r'star.txt',mode='rb') as f:
contents = f.read(3) # 读取三个字符
contents2 = f.read() # 后面再读就会继续先后读取
print(contents)
print(contents2)
# 输出结果:
b'\xe9\xab\x98'
b'\xe5\x9c\x86\xe5\x9c\x86\r\n\xe8\xb4\xbe\xe9\x9d\x99\xe9\x9b\xaf\r\n\xe5\x91\xa8\xe8\x8a\xb7\xe8\x8b\xa5\r\n\xe8\xb5\xb5\xe6\x95\x8f'
readline(): 每次只读取一行
with open(r'star.txt',mode='r',encoding='utf-8') as f:
contents1 = f.readline()
contents2 = f.readline()
contents3 = f.readline()
contents4 = f.readline()
print(contents1)
print(contents2)
print(contents3)
print(contents4)
# 输出结果
高圆圆
贾静雯
周芷若
赵敏
with open(r'star.txt',mode='r',encoding='utf-8') as f:
contents1 = f.readline().strip()
contents2 = f.readline().strip()
contents3 = f.readline().strip()
contents4 = f.readline().strip()
print(contents1)
print(contents2)
print(contents3)
print(contents4)
# 输出结果
高圆圆
贾静雯
周芷若
赵敏
readlines():将每一行形成一个元素,放到一个列表中
with open(r'star.txt',mode='r',encoding='utf-8') as f:
contents = f.readlines()
print(contents)
# 输出结果
['高圆圆\n', '贾静雯\n', '周芷若\n', '赵敏']
小结:
with open(r'star.txt',mode='r',encoding='utf-8') as f:
print(f.read()) # 这样就是将文件一次性全部读取到内存中,内存容易崩溃
with open(r'star.txt',mode='r',encoding='utf-8') as f:
for line in f:
print(line) # 这种方式就是在一行一行的进行读取
# print(line.strip())
# 等价于下面实现的功能
with open(r'star.txt',mode='r',encoding='utf-8') as f:
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
覆盖写
with open(r'msg.txt',mode='w',encoding='utf-8') as f:
f.write('789')
f.flush()
with open(r'msg.txt',mode='wb') as f:
msg = '你好'.encode('utf-8')
f.write(msg)
f.flush()
追加
with open(r'msg.txt',mode='a',encoding='utf-8') as f:
f.write('666')
f.flush()
with open(r'msg.txt',mode='ab') as f:
msg = '再见'.encode('utf-8')
f.write(msg)
f.flush()
对于读写模式,必须是先读后写,因为光标默认在开头位置,当读完了以后再进行写入。其中,使用频率最高的模式就是r+。
r+模式
msg.txt 中内容:清风不识字,何必乱翻书
(1)正确的操作(先读后写):
with open(r'msg.txt',mode='r+',encoding='utf-8') as f:
contents = f.read()
print(contents) # 清风不识字,何必乱翻书
f.write('你好')
# f.write('\n你好')
f.flush()
with open(r'msg.txt',mode='r',encoding='utf-8') as f2:
ret = f2.read()
print(ret) # 清风不识字,何必乱翻书你好
(2)错误的操作(先写后读):
with open(r'msg.txt',mode='r+',encoding='utf-8') as f:
f.write('再见')
f.flush()
contents = f.read()
print(contents) # 不识字,何必乱翻书
with open(r'msg.txt',mode='r',encoding='utf-8') as f2:
ret = f2.read()
print(ret) # 再见不识字,何必乱翻书
注意:
在r+模式下,如果读取了内容,不论读取内容多少,光标显示的是多少,再写入或者操作文件的时候都是在文件的末尾进行的操作。
w+ 模式
先将所有的内容清空,然后写入,最后读取,但是读取的内容是空的,w+模式不常用。
with open(r'msg.txt',mode='w+',encoding='utf-8') as f:
f.write('你好') # 将文件内容清空,写入“你好”,但后面的读取获取不到数据,为空
f.flush()
contents = f.read()
print(contents) # 结果是空,读不到数据
追加读 a+,a+b
with open(r'msg.txt',mode='a+',encoding='utf-8') as f:
f.write('你好') # 在文件末尾追加“你好”,即:清风不识字,何必乱翻书你好
f.flush()
contents = f.read()
print(contents) # 结果是空,读不到数据
def write_file(filename,contents):
with open(filename,mode='w',encoding='utf=8') as f:
f.write(contents)
f.flush()
def read_file(filename):
with open(filename,mode='r',encoding='utf-8') as f2:
msg = f2.read()
return msg
filename = r'msg.txt'
ret = read_file(filename) # msg.txt中的内容为“清风不识字,何必乱翻书”
new_str = "春风"
old_str = "清风"
result = ret.replace(old_str,new_str)
write_file(filename,result) # msg.txt中的内容修改为“春风不识字,何必乱翻书”
seek()
with open(r'msg.txt',mode='r+',encoding='utf-8') as f:
contents = f.read() # 读取文件内容,此时光标移动到末尾
print(contents)
print(f.tell()) # 222
f.seek(0) # 将光标移动到开头
print(f.tell()) # 0
f.seek(0,2) # 将光标移动到结尾
print(f.tell()) # 222
msg = f.read()
print(msg) # 读取文件内容,什么都没有
f.seek(0) # 再次将光标移动到开头
f.write("辛弃疾") # 在开头覆盖写“辛弃疾”,此时光标在9,中文3*3个=9
print(f.tell()) # 9
f.flush()
tell()
truncate()
with open(r'msg.txt',mode='w',encoding='utf-8') as f:
f.write("哈哈") # 写入两个字符
f.seek(3) # 光标移动到3,也就是两个字符中间
f.truncate() # 删除光标之后的所有内容
with open(r'msg.txt',mode='r+',encoding='utf-8') as f:
contents = f.read(3) # 读取9个字符
print(contents) # “哈哈哈”
f.seek(4) # 光标移动至4,当truncate()指定截取字符长度,仍从开头进行截取,和光标移动位置无关
f.truncate(9) # 截取9个字符,即:保留开头前9个字符,后面的全删除
当发生异常时,需要对异常进行捕获,然后再进行相应的处理。python的异常捕获常用try/except结构,把可能发生错误的语句放在try模块里,用except来处理异常;每一个try都必须对应至少一个except。此外,与python异常相关的关键字主要有:
关键字 | 关键字说明 |
---|---|
try/except | 捕获异常并处理 |
pass | 忽略异常 |
as | 定义异常实例(except MyError as e) |
else | 如果try中的语句没有引发异常,则执行else中的语句 |
finally | 无论是否出现异常,都执行的代码 |
raise | 抛出/引发异常 |
(1)捕获所有异常
try:
<语句>
except:
print('异常说明')
(2)捕获指定异常
try:
<语句>
except <异常名>:
print('异常说明')
try:
<语句>
except Exception:
print('异常说明')
try:
f = open("file-not-exists", "r")
except IOError as e:
print("open exception: %s: %s" %(e.errno, e.strerror))
# 运行结果 open exception: 2: No such file or directory
(3)捕获多个异常
try:
<语句>
except (<异常名1>, <异常名2>, ...):
print('异常说明')
try:
<语句>
except <异常名1>:
print('异常说明1')
except <异常名2>:
print('异常说明2')
except <异常名3>:
print('异常说明3')
(4)异常中的else
try:
<语句>
except <异常名1>:
print('异常说明1')
except <异常名2>:
print('异常说明2')
else:
<语句> # try语句中没有异常则执行此段代码
(5)异常中的finally
try:
<语句>
finally:
<语句>
str1 = 'hello world'
try:
int(str1)
except IndexError as e:
print('IndexError:',e)
except KeyError as e:
print('KeyError:',e)
except ValueError as e:
print('ValueError:',e)
else:
print('try内没有异常')
finally:
print('无论异常与否,都会执行finally')
# 运行结果:
# ValueError: invalid literal for int() with base 10: 'hello world'
# 无论异常与否,都会执行finally
(6)raise主动触发异常
raise [Exception [, args [, traceback]]]
def not_zero(num):
try:
if num == 0:
raise ValueError('参数错误')
return num
except Exception as e:
print(e)
not_zero(0)
# 运行结果 参数错误
(7)采用traceback模块查看异常
一个例子
try:
1/0
except Exception as e:
print(e)
# 运行结果 division by zero
import traceback
try:
1/0
except Exception as e:
traceback.print_exc()
# 运行结果
# Traceback (most recent call last):
# File "c:/Users/dpp/Desktop/csdn/except.py", line 57, in
# 1/0
# ZeroDivisionError: division by zero
traceback.print_exc(file=open('except.txt','w+'))
dump
import json
dic = {
'name':'Lucy','age':18}
with open('nums.json','w',encoding='utf-8') as f:
json.dump(dic,f)
load
with open('nums.json','r',encoding='utf-8') as f2:
f_dic = json.load(f2)
print(f_dic,type(f_dic)) # {'name': 'Lucy', 'age': 18}
print(f_dic.get('name')) # Lucy
dumps
import json
dic = {
'name':'Lucy','age':18}
f_dic = json.dumps(dic)
print(f_dic,type(f_dic)) # {"name": "Lucy", "age": 18}
loads
import json
dic = {
'name':'Lucy','age':18}
f_dic = json.dumps(dic)
print(f_dic,type(f_dic)) # {"name": "Lucy", "age": 18}
d_dic = json.loads(f_dic)
print(d_dic,type(d_dic)) # {'name': 'Lucy', 'age': 18}
dump
import pickle
import time
struct_time = time.localtime()
print(struct_time) # time.struct_time(tm_year=2021, tm_mon=1, tm_mday=29, tm_hour=11, tm_min=38, tm_sec=46, tm_wday=4, tm_yday=29, tm_isdst=0)
with open('nums.json','wb') as f:
pickle.dump(struct_time,f)
load
import pickle
import time
struct_time = time.localtime()
print(struct_time) # time.struct_time(tm_year=2021, tm_mon=1, tm_mday=29, tm_hour=11, tm_min=38, tm_sec=46, tm_wday=4, tm_yday=29, tm_isdst=0)
with open('nums.json','rb') as f2:
ret = pickle.load(f2)
print(ret) # time.struct_time(tm_year=2021, tm_mon=1, tm_mday=29, tm_hour=11, tm_min=38, tm_sec=46, tm_wday=4, tm_yday=29, tm_isdst=0)
print(ret.tm_year) # 2021
print(type(ret)) #
dumps
import pickle
dic = {
"name":"Lucy","age":"18"}
str_dic = pickle.dumps(dic)
print(str_dic)
loads
import pickle
dic = {
"name":"Lucy","age":"18"}
str_dic = pickle.dumps(dic)
print(str_dic) # b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x04\x00\x00\x00Lucyq\x02X\x03\x00\x00\x00ageq\x03X\x02\x00\x00\x0018q\x04u.'
print(type(str_dic)) #
dic2 = pickle.loads(str_dic)
print(dic2) # {'name': 'Lucy', 'age': '18'}
print(type(dic2)) #