找文件排序

#!/usr/bin/env python

#encoding: utf-8

#author: xiaofangliu

importre

importsys

importshlex

importtime

importdatetime

importsubprocess

defexecute_command(cmdstring,cwd=None,timeout=None,shell=False):

"""执行一个SHELL命令

        封装了subprocess的Popen方法, 支持超时判断,支持读取stdout和stderr

        参数:

      cwd: 运行命令时更改路径,如果被设定,子进程会直接先更改当前路径到cwd

      timeout: 超时时间,秒,支持小数,精度0.1秒

      shell: 是否通过shell运行

    Returns: return_code

    Raises: Exception: 执行超时

"""

res={}

ifshell:

cmdstring_list=cmdstring

else:

cmdstring_list=shlex.split(cmdstring)

iftimeout:

end_time=datetime.datetime.now()+datetime.timedelta(seconds=timeout)

sub=subprocess.Popen(cmdstring_list,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

whilesub.poll()isNone:

line=sub.stdout.read()

line=line.strip()

tmp=line.split("\n")

time.sleep(0.1)

#print 'line', line

iftimeout:

ifend_time<=datetime.datetime.now():

raiseException("Timeout:%s"%cmdstring)

res['status']=str(sub.returncode)

res['data']=tmp

returnres

deffind_file(find_path):

res=execute_command('ls -l%s'%find_path)

res_test=['1525322118686_082521','1525322118687_082825','1525336170089_082906','1525343215147_073302','1525343215150_082207','1525411322548_070801','1525411322548_073212','1525411322549_072723','1525411322549_074115','1525411322549_074343','1525411322549_075055','1525411322549_075243','1525411322549_080431','1525411322549_080651','1525411322549_080802','1525411322550_074831','1525411322550_204831','test.py']

print'++++', res

defrun_sort(find_path,data):

res=[]

foriindata:

same=[]

forcindata:

ifi.split("_")[0]==c.split("_")[0]:

_i=i.split("_")[1]

i_str=re.sub(r"\b0*([1-9][0-9]*|0)",r"\1",str(_i))

same.append(int(i_str))

same=set(same)

biggest=sorted(same)[-1]

_one=find_path+'/'+biggest

        res.append(_one)

returnres

#存在问题,dest_path 目标目录要自动生成 不能写死 !!!需求有异 要改

defmove_mp4(src_path,dest_path):

res=[]

foriinsrc_path:

file_path=execute_command('find%s-name "*.mp4"'%i)

tmp=execute_command('mv%s%s'%file_path, dest_path)

        res.append(tmp)

returnres

defrun_script():

find_path=sys.argv[1]

next_path_prefix=sys.argv[2]

dest_path=sys.argv[3]

file_list=find_file(find_path)

iffile_list:

tmp=run_sort(find_path, file_list)

iftmp:

res=move_mp4(tmp, dest_path)

returnres

if__name__=='__main__':

    run_script()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.创建文件夹 !/bin/sh mkdir -m 777 "%%1" 2.创建文件 !/bin/sh touch...
    BigJeffWang阅读 10,198评论 3 53
  • 农历四月末五月初,是麦黄季节。田家少闲月,五月人倍忙。每到这个季节,田野间,被高高的日头普照,干热风吹拂着麦...
    乔延宾阅读 1,620评论 3 5
  • 中国有句俗语:“做事先做人”,说的就是人际关系的处理。同样在职场工作里,人际关系也起着十分重要的作用,在英文中单词...
    小小斜阅读 1,789评论 0 1
  • 谈钱伤感情,谈感情伤钱。 仿佛钱和感情真的水火不相容。最甜蜜和最神圣的感情莫过于爱情。爱情这玩意儿,在我的记忆中,...
    余老诗写作课阅读 923评论 3 42
  • 李丽【日精进打卡第139天】 【知~学习】 《六项精进》2遍 共160遍 《大学》2遍 共196遍 【经典名句分享...
    李知言阅读 119评论 0 0