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

【Python】多线程实现批量文件移动,第1张

概述引言编写Python脚本实现将一个文件夹中的所有文件按照修改日期(xxxx年xx月xx日)分类移动。依赖pipinstallalive_progress代码importosimporttimeimportshutilimportconcurrent.futuresascffromalive_progressimportalive_bar#pipinstallalive_progr 引言

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

依赖
pip install alive_progress 
代码
import osimport timeimport shutilimport concurrent.futures as cffrom alive_progress import alive_bar  # pip install alive_progressdef main():    name_List = os.Listdir()    name_List.remove(os.path.basename(__file__))    with alive_bar(len(name_List)) as bar: # 进度条        with cf.ThreadPoolExecutor() as p: # 线程池            for name in name_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(lambda func: bar())if __name__ == '__main__':    main()
参考

https://docs.python.org/zh-cn/3.11/library/concurrent.futures.html

总结

以上是内存溢出为你收集整理的【Python】多线程实现批量文件移动全部内容,希望文章能够帮你解决【Python】多线程实现批量文件移动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1186449.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存