Python多线程实现批量文件移动

引言

编写Python脚本实现将一个文件夹中的所有文件按照修改日期(xxxx年xx月xx日)分类移动。

依赖

pipinstallalive_progress代码

importosimporttimeimportshutilimportconcurrent.futuresascffromalive_progressimportalive_bar#pipinstallalive_progressdefmain():name_list=os.listdir()name_list.remove(os.path.basename(__file__))withalive_bar(len(name_list))asbar:#进度条withcf.ThreadPoolExecutor()asp:#线程池fornameinname_list:time_stamp=os.path.getmtime(name)#获取文件修改日期时间戳time_local=time.localtime(time_stamp)#将时间戳转换为本地时间time_style=time.strftime(r%Y%m%d,time_local)#将本地时间转换为指定格式的时间os.makedirs(time_style,exist_ok=True)p.submit(shutil.move,name,time_style).add_done_callback(lambdafunc:bar())if__name__==__main__:main()参考



转载请注明地址:http://www.sanbaicaoasb.com/scry/8518.html
  • 上一篇文章:
  • 下一篇文章:
  • 热点文章

    • 没有热点文章

    推荐文章

    • 没有推荐文章