练习题
Posted caopeiyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习题相关的知识,希望对你有一定的参考价值。
#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)
以上是关于练习题的主要内容,如果未能解决你的问题,请参考以下文章