"""提示:代码中的内容均被注释,请参考,切勿照搬"""
1 #文件的打开和关闭
2 """
3 文件对象 = open("文件名","使用方式")4 rt:读取一个txt文件5 wt: 只写打开一个txt文件,(如果没有该文件则新建该文件)会覆盖原有内容6 at:打开一个txt文件,并从文件指针位置追加写内容(文件指针默认在末尾)7 文件操作错误属于:I/O异常8 通常的异常:9 try:10 f = open("a.txt","wt")11 except Exception as e:12 print(e)13 """
14 #文件的写操作
15 #函数: 文件对象.write(s)其中s是待写入文件的字符串{文件对象需要时可写入的对象}
16 """
17 try:18 fobj = open("anc.txt","wt") #wt:可写入操作方式/at为在原有的文件内容追加写入19 fobj.write(" more") #写函数20 fobj.close()21
22 except Exception as err:23 print(err)24
25 结果:anc文件保存至当前目录下,并写入“[换行]more”26 """
27 #案例:学生信息储存
28 """
29 name = "wanzi"30 gender = "男"31 age = 2332 try:33 f = open("students.txt","wt")34 while True:35 #s = Student(i)36 #if s:37 f.write("namegenderge")38 ans = input("continue(Y/y):")39 if ans != "Y" and ans != "y":40 break41 i = i+142 f.close()43
44 except Exception as e:45 print(e)46
47 """
48 #读文件操作 文件对象.read(n) //返回全部字符串或者n字节字符
49 """
50 def writeFile(): #写文件操作51 f &