python实例:公司应用(一)
需求:公司是做在线教育的,新谈的一家公司帮忙做视频录制,这个公司的录制是直接录到服务器上的,但是录制完之后的视频目录名称和视频名都不符合我们的要求,每次同事都得手动改,所以写了下边脚本,希望大家可以参考。有好的想法也可以提出来
#!/usr/bin/env python
# *-coding:UTF-8-*-
import re
import os
import shutil
import time
import datetime
dirs = []
riqi = time.strftime("%Y%m%d",time.localtime(time.time()))
date = riqi[4:]
mulu = "/kejiandata/test/gensee/"
def getDirs():
"""get dirs which we should be handle,for example jsd1606a"""
os.chdir("/kejiandata/record")
all=os.listdir(".")
for dir in all:
pattern1 = re.compile("[A-Z][0-9]?[a-z]{1,2}[0-9]{4}a?")
pattern2 = re.compile("w[a-z]{3}[0-9]{4}a?")
match1 = pattern1.match(dir)
match2 = pattern2.match(dir)
if match1 or match2:
dirs.append(dir)
return dirs
def main(dirs):
"""get Dirs'son directory name,for example Jsd1606a/20161014010135"""
for Sdir in dirs:
os.chdir("/kejiandata/record/"+Sdir)
Sdirs = os.listdir(".")
zhuanye = Sdir.lower()
Flv = []
for videoDir in Sdirs:
if videoDir[:8] == riqi:
Flv.append(videoDir)
print Flv
if not Flv:
print "今天没有录制视频"
for flv in Flv:
if len(flv) == 14:
os.chdir("/kejiandata/record/"+Sdir)
accessTime = os.path.getatime(flv)
Time = datetime.datetime.fromtimestamp(accessTime)
time = Time.strftime("%H%M")
if time[0] == 0:
time = int(time[1:])
else:
time = int(time)
if 830 < time < 1230:
flv_dir_name = riqi+"am"
elif 1320 < time < 1730:
flv_dir_name = riqi+"pm"
# elif 1730 < time < 2200:
# flv_dir_name = riqi+"pm2"
else:
shutil.copy(flv+os.sep+"recordas.flv",mulu+zhuanye+flv[4:]+".flv")
print flv+" dirname is already changed"
continue
try:
os.rename(flv,flv_dir_name)
except OSError:
with open("/kejiandata/record/zhongduan.txt","a") as f:
f.write(flv_dir_name+"出现中断\n")
continue
os.chdir("/kejiandata/record/"+Sdir+os.sep+flv_dir_name)
shutil.copy("recordas.flv",mulu+zhuanye+date+flv_dir_name[-2:]+".flv")
if __name__ == "__main__":
getDirs()
print dirs
main(dirs)
相关文章