
编写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】多线程实现批量文件移动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)