码迷,mamicode.com
首页 > 其他好文 > 详细

练习题

时间:2018-07-08 14:39:21      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:nbsp   format   exists   elf   自定义   exist   get   file_path   orm   

#1、pymysql、nnlog、redis、xlwt、xlrd、xlutils 把这些模块装好 

 

 

2、写一个程序,创建一些数据。

     1、创建10个文件夹,文件夹名字自定义  

    2、每个文件下面有10个 日志文件, 

            文件名是从今天开始的前10天 

                android_2018-07-01.log

                android_2018-06-30.log 

                android_2018-06-29.log 

                android_2018-06-28.log 

                android_2018-06-28.log 

                android_2018-06-28.log 

                android_2018-06-28.log  

    3、随机选3个文件,往里面写点东西  

3、写一个程序,把上面那些空文件删掉,还有3天前的删掉

import os,time,datetime,random
#创建文件夹
def makedir(path,num):
path = str(path).rstrip(‘\\‘)
if os.path.exists(path):
print("目录:%s已存在"%path)
return False
else:
for i in range(num):
os.makedirs(path+str(i))
return True
def timestampToStr(timestamp=None,format = ‘%Y-%m-%d‘):
if timestamp:
time_tuple = time.localtime(timestamp)
return time.strftime(format,time_tuple)
return time.strftime(format)
def strtoTimeStamp(str=None,format = ‘%Y-%m-%d‘):
if str:
time_tuple = time.strptime(str,format)
return time.mktime(time_tuple)
return time.time()
def timeDelate(num):
if num==0:
return time.time()
return time.time()-24*60*60*num
def makefile(path,num):
for cur, dir, file in os.walk(path):
for d in dir:
for i in range(num):
d_time = timestampToStr(timeDelate(i))
str = ‘_‘ + d_time + ‘.log‘
f = os.path.join(d) + str
file_path = os.path.join(cur, d)
file_name = file_path + ‘\\‘ + f
f = open(file_name, ‘w‘)
f.close()
def writeToFile(path,num):
for cur, dir, file in os.walk(path):
for d in dir:
get_path = os.path.join(cur, d)
f_choose = random.sample(os.listdir(get_path), num)
for f in f_choose:
fpath = os.path.join(cur, d)+‘\\‘+f
print(fpath)
with open(fpath, ‘w‘) as f:
f.write(‘test‘)
def delFile(path):
for cur, dir, file in os.walk(path):
for f in file:
file_path = os.path.join(cur, f)
if os.path.getsize(file_path) == 0:
os.remove(file_path)
else:
str = f.split(‘_‘)[1].split(‘.‘)[0]
if time.time() - strtoTimeStamp(str) > 60 * 60 * 24 * 3:
os.remove(os.path.join(cur, f))

f_path =‘logs\\name‘
path=‘logs‘
makedir(f_path,10)
makefile(path,10)
writeToFile(path,3)
delFile(path)


 

 

 

练习题

标签:nbsp   format   exists   elf   自定义   exist   get   file_path   orm   

原文地址:https://www.cnblogs.com/caopeiyang/p/9279852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!