说明
接着推进计划,先不讲形式,先实验起来
内容
有一个文件夹,里面有很多7z压缩包(文件夹名称是日期),希望以后放进新的压缩包时可以半自动的解压
- 1 这个任务很低频,所以其实也容易忘记
- 2 文件很多,我不想一一指定文件名
- 3 不想有额外的环境搭建,直接run一下就好
在文件夹里建立一个程序runtime.py ,内容如下:
'''
docker run -it\
-v /etc/localtime:/etc/localtime \
-v /home/jupyter_data:/workspace \
-v /etc/timezone:/etc/timezone\
-v /home/bigdisk:/home\
-e "LANG=C.UTF-8"\
YOUR_IMAGE\
bash
'''
import os
from pathlib import Path
from datetime import datetime
def get_folders_contains(fpath = None, special_char = None, verbose=True):
tier2_files = os.listdir(fpath)
res_list = []
for the_file in tier2_files:
the_file_tag = Path(fpath + the_file)
if the_file_tag.is_dir():
if special_char in the_file:
res_list.append(fpath + the_file + '/')
if verbose:
print('find %s folders' % len(res_list))
return res_list
def get_batch_file1(fpath, suffix, fetch_num, is_sorted =True):
flist = [x for x in os.listdir(fpath) if x.endswith(suffix)]
if is_sorted:
flist.sort()
return flist[:fetch_num]
cur_folders = get_folders_contains('./', '20')
cur_folders_from_file = [x.split('/')[-2].replace('-','') + '.7z' for x in cur_folders]
cur_files = get_batch_file1('./', '.7z', 100000)
gap_files = sorted(set(cur_files) - set(cur_folders_from_file))
if len(gap_files):
os.system('7z x %s' % gap_files[0])
for i in range(3):
cur_folders = get_folders_contains('./', '20')
cur_folders_from_file = [x.split('/')[-2].replace('-','') + '.7z' for x in cur_folders]
cur_files = get_batch_file1('./', '.7z', 100000)
gap_files = sorted(set(cur_files) - set(cur_folders_from_file))
if len(gap_files):
the_file_name = gap_files[0]
try:
print('>>> %s' % the_file_name)
os.system('7z x %s' % the_file_name)
status = True
except:
print('XXX %s Fail' % the_file_name)
status = False
finally:
the_time_str = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
the_res = ' '.join([the_file_name,str(status),the_time_str,'\n'])
with open('runtime.log', 'a') as f:
f.write(the_res)
if not status:
break
总结
- 1 可以使用简单的docker命令一步启动
- 2 没有参数化(因为任务确实比较简单)
- 3 增加了log
这样算是部分实践了前文的设想,从底向上,以解决问题为主,逐步的实现设计。
|