目录
1. try ... except ... else ... finally 排列 P231
2. write, read, seek, readline, readlines 基本文件操作 P245
3. os模块 基本目录操作 P249
4. os.path 模块 复杂目录操作 P250
5. os 模块 高级文件操作 P257
1. try ... except ... else ... finally 排列 P231


try:
#待测试的函数
except(必需,至少一个except)errorTypeA:(可选,不声明Type就是捕获所有error):
#对 errorTypeA 的处理
except errorTypeB:
#对 errorTypeB 的处理
......:
#......
else:
# 没有出现任何异常时,执行的操作
finally:
# 不管有没有异常,都会执行的操作
我们试一下各种情况
正确分苹果: try...else...finally

错误分苹果: try...except...finally

2. write, read, seek, readline, readlines 基本文件操作 P245


似乎,只能以只写方式打开文件来写入,以只读方式打开文件来读取。。。 读写方式无法同时读写?
file1 = open('message.txt','w') # 'a' 则会以追加方式打开
file1.close() # open close 成对现
file1.write(str1)
file1.seek(0)
str2 = file1.read()
str3 = file1.read(10)
line = file1.readline()
msg = file1.readlines() # 返回值是字符串列表
3. os模块 基本目录操作 P249


os.getcwd()
os.listdir()
os.mkdir()
os.rmdir()
os.makedirs()
os.removedirs()
os.chdir()
os.walk()
4. os.path 模块 复杂目录操作 P250


os.path.abspath()
os.path.exists()
os.path.basename()
os.path.dirname()
5. os 模块 高级文件操作 P257
文件权限操作

![]()
os.access()
os.rename()
os.chmod()
os.remove()
获取文件基本信息


fileinfo = os.stat() # 返回一个文件状态对象,该对象包含大量属性















![【洛谷 P8700】[蓝桥杯 2019 国 B] 解谜游戏 题解(字符串+映射+周期性)](https://img-blog.csdnimg.cn/img_convert/c1733f6b4f09b37cf356fb26028fffe0.png)


