一. 实验目的和要求
了解Pyhon中常见的异常
二. 实验环境
python 3.10 64-bit
三. 实验过程
实例1
代码如下:
1 print("\n",'='*10,"蚂蚁庄园动态","="*10) 2 file = open('message.txt','w') 3 print("\n 即将显示……\n")
实例2
代码如下:
1 print("\n", "="*10, "蚂蚁庄园动态", "="*10) 2 file = open('message.txt', 'w') 3 #写入动态信息 4 file.write("你使用了1张加速卡,小鸡擅起袖子开始双手吃饲料,进食速度大大加快。\n") 5 print("\n 写入了一条动态......\n") 6 file.close() #关闭文件对象 7 8 print("\n", "="*10, "蚂蚁庄园动态", "="*10) 9 file = open('message.txt', 'a') 10 #追加一条动态信息 11 file.write("mingri的小鸡在你的庄园待了22分钟,吃了6g饲料之后,被你赶走了。\n") 12 print("\n 追加了一条动态......\n") 13 file.close()
实例3
代码如下:
1 print("\n", "="*25, "蚂蚁庄园动态", "="*25) 2 with open('message.txt', 'r') as file: #打开保存蚂蚁庄园动态信息的文件 3 message = file.read() #读取全部动态信息 4 print(message) 5 print("\n", "="*29, "over", "="*29, "\n")
实例4
代码如下:
print("\n", "="*35, "蚂蚁庄园动态", "="*35) with open('message.txt', 'r') as file: #打开保存蚂蚁庄园动态信息的文件 number = 0 while True: number += 1 line = file.readline() if line == '': #空白行 break print(number, line, end="\n") #输出一行内容 print("\n", "="*39, "over", "="*39, "\n")
实例5
代码如下:
1 import os 2 path = "C:\\demo" 3 print("【",path,"】目录下包括的文件和目录:") 4 for root,dirs,files in os.walk(path,topdown=True): 5 for name in dirs: 6 print("●",os.path.join(root,name)) 7 for name in files: 8 print("◎",os.path.join(root,name))
实例6
代码如下:
1 import os 2 fileinfo = os.stat("mr.png") 3 print("文件完整路径:",os.path.abspath("mr.png")) 4 # 输出文件的基本信息 5 print("索引好:",fileinfo.st_ino) 6 print("设备名:",fileinfo.st_dev) 7 print("文件大小:",fileinfo.st_size,"字节") 8 print("最后一次访问时间:",fileinfo.st_atime) 9 print("最后一次修改时间:",fileinfo.st_mtime) 10 print("最后一次状态变化时间:",fileinfo.st_ctime)
标签:10,Python,file,print,fileinfo,第九,os,message From: https://www.cnblogs.com/yisheng8/p/16921790.html