首页 > 其他分享 >13 刘欣晨 第十章实例+实战

13 刘欣晨 第十章实例+实战

时间:2022-12-30 15:45:42浏览次数:40  
标签:13 庄园 name 刘欣晨 第十章 项目名称 file print fileinfo

实验 一  项目名称:      创建并打开记录蚂蚁庄园动态的文件

print("\n","="*10,"蚂蚁庄园动态","="*10)
file = open('message.txt','w')
print("\n 即将显示···\n")

实验 二 项目名称:      向蚂蚁庄园的动态文件写入一条信息

print("\n","="*10,"蚂蚁庄园动态","="*10)
file = open('message.txt','a')
file.write("mingri的小鸡在你的庄园待了22分钟,吃了6g饲料之后,被你该走了。\n")
print("\n 追加了一条动态····")
file.close()

实验 三  项目名称:      显示蚂蚁庄园的动态

print("\n","="*25,"蚂蚁庄园动态","="*25,"\n")
with open('message.txt','r') as file:
  message = file.read()
  print(message)
  print("\n","="*29,"over","="*29,"\n")

实验 四  项目名称:      逐行显示蚂蚁庄园的动态

print("\n","="*35,"蚂蚁庄园动态","="*35,"\n")
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")

实验 五  项目名称:      遍历指定目录

import os
path = "C:\\demo"
print("【",path,"】目录下包括的文件和目录:")
for root, dirs, files in os.walk(path, topdown=True):
  for name in dirs:
    print("●",os.path.join(root, name))
  for name in files:
    print("◎",os.path.join(root, name))

实验 六  项目名称:      获取文件基本信息

import os
fileinfo = os.stat("mr.png")
print("文件完整路径报告",os.path.abspath("mr.png"))
print("索引号:",fileinfo.st_ino)
print("设备名:",fileinfo.st_dev)
print("文件大小:",fileinfo.st_size,"字节")
print("最后一次访问时间:",fileinfo.st_atime)
print("最后一次修改时间:",fileinfo.st_mtime)
print("最后一次状态变化时间:",fileinfo.st_ctime)

实战 一  项目名称:      根据当前时间创建文件

import time
def create():
  global name
  localTime = time.strftime("%Y%m%d%H%M%S",time.loacaltime())
  name = localTime + '.txt'
  with open(name,'a')as file:
    pass
if __name__ == '__main__':
  amount = int(input("请输入需要生成的文件数:"))
  for i in range(amount):
    create()
    print('file ' + str(i+1) +': ' +name)
    time.sleep(1)
  print('生成成功!')

 

标签:13,庄园,name,刘欣晨,第十章,项目名称,file,print,fileinfo
From: https://www.cnblogs.com/L13538196467/p/17015024.html

相关文章