无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。人工智能教程
场景描述:
针对不同机器的同一个日期文件夹下的图片文件合并在一起,并自动打包压缩、
2019-10-09 这个目录下 有M0,M1,M2,M3,M4,M5文件夹,目标需要把M0类的移动到 normal 文件夹,M1,M2,M3,M4,M5类的移到spam文件夹下。然后把这2019-10-09这个终极文件夹打包压缩。
# -*- coding:utf-8 -*-
import datetime
import os
import shutil
machine1='dt1'
machine2='dt2'
bak_dir = '/home/admin/zhongkui_spam/services/img_services/zk_image_async_spam/corpus/'
result1='normal'
result2='spam'
result3='middle'
def getYesterday():
"""
:return: 获取昨天日期
"""
today = datetime.date.today()
oneday=datetime.timedelta(days=1)
yesterday=today-oneday
# 日期转字符串
partition_date=yesterday.strftime('%Y-%m-%d')
return partition_date
dt = getYesterday()
def zk_makedirs(bak_dir,dt,result):
"""
:param bak_dir: 路径
:param dt: 日期
:param result: 文件夹名字
:return: 创建文件夹
"""
# 如果不存在就创建文件
p = os.path.join(bak_dir, dt, result)
if not os.path.exists(p):
os.makedirs(p)
return p
def excute_cmd(cmd):
"""
:param cmd: 执行cmd 命令
:return:
"""
try:
res = os.popen(cmd)
res1_1 = res.read()
res.close()
except Exception as e:
pass
return res1_1
def zk_rename_move(p3,machine,M,p1):
"""
:param p3: 文件夹
:param machine: 机器
:param M: 类型
:return:
"""
# 重命名文件夹下所有文件
filepath = p3
for parent, dirnames, filenames in os.walk(filepath):
for filename in filenames:
res_split = filename.split('.')
new_file_name = res_split[0] + '%s_%s.' % (M,machine) + res_split[1]
os.chdir(filepath)
os.rename(filename, new_file_name)
shutil.move(new_file_name, p1)
def tar_file(date):
"""
:param date: 打包文件夹名
:return:
"""
cmd=' tar -zcvP -f %s.tar.gz %s/' %(date,date)
res = os.popen(cmd)
res1= res.read()
res.close()
if __name__ == '__main__':
p1=zk_makedirs(bak_dir,dt,result1)
p2=zk_makedirs(bak_dir,dt,result2)
p3=zk_makedirs(bak_dir,dt,result3)
M=['M0','M1','M2','M3','M4','M5']
try:
for each in M:
cmd1='scp [email protected]:/home/admin/zhongkui_spam/services/img_services/zk_image_async_spam/result/%s/%s/* %s/' %(dt,each,p3)
cmd2='cp /home/admin/zhongkui_spam/services/img_services/zk_image_async_spam/result/%s/%s/* %s/' %(dt,each,p3)
# 第一台
res1_1 = excute_cmd(cmd1)
if each=='M0':
zk_rename_move(p3,each,machine2,p1)
else:
zk_rename_move(p3,each,machine2,p2)
# 第二台
res2_1 = excute_cmd(cmd2)
if each == 'M0':
zk_rename_move(p3,each,machine1, p1)
else:
zk_rename_move(p3,each,machine1, p2)
except Exception as e:
pass
shutil.rmtree(p3)
# 打包命令
os.chdir(bak_dir)
date=dt
tar_file(date)