一、创建并打开记录蚂蚁庄园动态的文件

print(“\n”,”=”*10,”蚂蚁庄园动态”,”=”*10)
film=open(‘message.txt’,’w’)
print(“\n即将显示……\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(“\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)

 

 

实战1、根据当前时间创建文件

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(“生成文件成功!”)

 

 实战2、批量添加文件夹

mport os
path = “C:\\test”
number = int(input(“请输入需要生成的文件夹个数:”))
for i in range(number):
ml = str(i+1)
os.makedirs(path + “\\” + ml)
print(“文件夹 ” + ml + ” 创建成功!”)

 

 

原文地址:http://www.cnblogs.com/666k/p/16882550.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性