第十章作业

实例01 

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

 

 

 

 

 

 

实例 02

print("\n","="*10,"蚂蚁庄园动态","="*10)
file = open('message.txt','w')
file.write("你使用了1张加速卡,小鸡撸起袖子开始双手吃饲料,进食速度大大加快。\n")
print("\n 写入了一条动态······\n")
file.close()

 

 

*追加动态:

print("\n","="*10,"蚂蚁庄园动态","="*10)
file = open('message.txt','a')
file.write("你的小鸡在QQ的庄园待了27分钟,吃了8g饲料被庄园主人赶回来了。")
print("\n 追加了一条动态······\n")
file.close()

 

 

 

实例03

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

 

 

 

 

 

 

实例04

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")

 

 

 

实例05 

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))

 

 

 

 

实例06

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_atime)
print("最后一次状态变化时间:",fileinfo.st_ctime)

 

 

 

对数值格式化:

import os

def formatTime(longtime):
'''格式化日期时间的函数
longtime:要格式化的时间
'''
import time
return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(longtime))
def formatByte(number):
'''格式化文件大小的函数
number:要格式化的字节数
'''
for(scale,label) in [(1024*1024*1024,"GB"),(1024*1024,"MB"),(1024,"KB")]:
if number>=scale:
return "%.2f %s" %(number*1.0/scale,label)
elif number == 1:
return "1 字节"
else:
byte = "%.2f" % (number or 0)
return (byte[:-3] if byte.endswith('.00') else byte)+" 字节"
if __name__ == '__main__':
fileinfo = os.stat("mr.png")
print("文件完整路径:",os.path.abspath("mr.png"))
print("索引号:",fileinfo.st_ino)
print("设备名:",fileinfo.st_dev)
print("文件大小:",formatByte(fileinfo.st_size))
print("最后一次访问时间:",formatTime(fileinfo.st_atime))
print("最后一次修改时间:",formatTime(fileinfo.st_atime))
print("最后一次状态变化时间:",formatTime(fileinfo.st_ctime))

 

 

实战一

import time
def create():
global name
localTime = time.strftime("%Y%m%d%H%M%S", time.localtime())
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('生成文件成功!')

 

 

 

 

 

实战二

import os
path = 'D:\大二\Camille\第十章'
def folders(number):
for i in range(1, number + 1):
folder_name = str(i)
if os.path.exists(folder_name):
print("已存在!")
else:
os.makedirs(folder_name)
if os.path.exists(folder_name):
print("文件"+str(i)+"创建成功!")
if __name__ == '__main__':
number = int(input("要生成的文件夹数量:"))
folders(number)

 

 

 

posted @ 2022-11-10 13:55  Camille_xx  阅读(19)  评论(0)    收藏  举报