python新建以时间命名的目录
2024-08-27 13:22:31
1.新建三级目录,第一级是去年的年份,第二级是去年的月,第三级为去年的日,在日的文件中写入今年的时分秒
import os
import time
import shutil
def create_file(path):
gettime=time.localtime() #获得当前时间的列表
getyear=gettime.tm_year-1 #获取去年的时间
getmon=gettime.tm_mon-1 #获得去年月
getday=gettime.tm_mday-1 #获得去年的日
if getmon==0: #如果为0就重置
getmon=12
setpath=path+str(getyear)
setpath2=setpath+"\\"+str(getmon)+"\\"+str(getday)
print setpath2 try:
if(os.path.exists(setpath)):
print u"文件已存在"
# 目前删除目录还有问题,
# print u"子目录",filelist
# os.removedirs(setpath2); 空目录删除成功
filelist=os.listdir(setpath2)
print "filelist",filelist
for i in filelist:
filepath=os.path.join(setpath2,i)
print u"filepath",filepath
if os.path.isfile(setpath2):
# os.remove(filepath)
os.removedirs(setpath2)#空目录删除成功
elif os.path.isdir(filepath):
print ""
os.rmdir(filepath)
#shutil.rmtree(filepath,True)#好凶残,直接弹框提示,删除也是成功的 '''
for root,dirs,files in os.walk(filepath):
for name in dirs:
os.rmdir(os.path.join(root,name))
print "2"
for name in files:
os.remove(os.path.join(root,name))
print "3"
#print u"文件已存在,已执行删除"
'''
else:
os.mkdir(path+str(getyear))
os.chdir(path+str(getyear))
os.mkdir(str(getmon))
os.chdir(str(getmon))
os.mkdir(str(getday))
os.chdir(str(getday))
filename=time.strftime('%H-%M-%S',time.localtime(time.time()))+".txt"
with open(filename,"w") as fp:
content=u"今天是今年的第 %s 天,今年的第 %s 个星期" %(gettime.tm_yday,time.strftime("%U",gettime)) #格式输出
fp.write(content.encode("utf-8"))
except Exception,e:
print e create_file("D:\\")
主要是时间函数 import time的使用
最新文章
- Linear Algebra lecture 2 note
- MongoDB修改器总结
- java获取对象属性类型、属性名称、属性值 【转】
- CodeForces 157C Message
- fixed应用
- 逆向实战第一讲,寻找OllyDbg调试工具的Bug并修复
- Hadoop 设置任务执行的队列以及优先级
- Python快速学习02:基本数据类型 &; 序列
- python多重继承的钻石问题
- python 可迭代对象
- 【Codeforces 696D】Legen...
- 搜索Ex
- 搞定 Kubernetes 基于flannel 的集群网络
- umeng友盟消息推送功能集成
- Ubuntu下tensorboard的使用
- 上手并过渡到PHP7(3)——Uniform Variable Syntax到底统一了什么
- oracle实例内存(SGA和PGA)调整-xin
- Django WSGI,MVC,MTV,中间件部分,Form初识
- tomcat注册成windows系统服务
- python3+Appium自动化02-Capability配置
热门文章
- vue+vuex+axios从后台获取数据存入vuex,组件之间共享数据
- 【angular5项目积累总结】自定义管道 OrderBy
- 7、包装类、System、Math、Arrays、大数据运算
- 撩课-Python-每天5道面试题-第6天
- Linux 中文乱码
- Oracle数据库基本操作 (五) —— 使用java调用存储过程
- Inter网关做Team的方法
- 原型链继承中的prototype、__proto__和constructor的关系
- 移动前端调试页面–weinre
- android studio 3.0 以上 查看sharedpreference