Python第十章作业
实例一:
print("\n","="*10, "蚂蚁庄园动态", "="*10)
file = open('message.txt', 'w')
print("即将显示......\n")

实例二:
print("\n", "="*10, "蚂蚁庄园动态", "="*10)
file = open('message.txt','w')
# 写入一条动态信息
file.write("你使用了1张加速卡,小鸡撸撸起袖子开始双手吃饲料,进食速度大大加快。\n")
print("\n写入了一条动态......\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:\\Windows"
print("【", path, "】目录下包括的文件和目录")
for root, dirs, files in os.walk(path, topdown=True):
for name in dirs:
print("0", os.path.join(root, name))
for name in files:
print("1", os.path.join(root, name))

实例六:
import os
fileinfo = os.stat("message.txt")
print("文件完整路径:", os.path.abspath("message.txt"))
# 输出文件的基本信息
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
import os
import datetime
num = int(input("请输入需要生成的文件数:"))
for i in range (num):
t = datetime.datetime.now()
path = "D:\\aasd\\mypythontest"
file = os.path.join(path, t.strftime('%Y%m%d%H%M%S')+'.txt')
open(file, 'w')
time.sleep(1)
i += 1
print("file " + str(i) + ":" + str(file))
print("生成文件成功!")

import os
num = int(input("请输入需要生成的文件数:"))
for i in range(num):
path = "D:\\aasd\\mypythontest"
if not os.path.exists('{}'.format(i + 1)):
os.mkdir('{}'.format(i + 1))
print("文件夹{}".format(i + 1) + "创建成功!")


浙公网安备 33010602011771号