Warning: Undefined array key "night" in /www/wwwroot/dhaomu.com/wp-content/themes/b2/header.php on line 18

Python技术根据文件的修改时间创建目标目录

Python技术:根据文件的修改时间(年月格式)创建目标目录,并将文件移动到相应的目标目录中。

Python技术:根据文件的修改时间(年月格式)创建目标目录,并将文件移动到相应的目标目录中。

代码演示

import os  
import shutil  
import datetime  
  
# 源目录和目标目录  
src_dir = 'D:/source_directory'  
dst_dir = 'D:/destination_directory'  
  
# 获取源目录中的所有文件  
files = [f for f in os.listdir(src_dir) if os.path.isfile(os.path.join(src_dir, f))]  
  
# 遍历文件列表  
for file in files:  
    # 构建源文件路径和目标文件路径  
    src_file = os.path.join(src_dir, file)  
    dst_file = os.path.join(dst_dir, file)  
      
    # 获取文件的修改时间  
    mod_time = os.path.getmtime(src_file)  
    mod_time_dt = datetime.datetime.fromtimestamp(mod_time)  # 将秒数转换为datetime对象  
      
    # 获取文件的年月格式的修改时间  
    year_month = mod_time_dt.strftime('%Y年%m月')  
      
    # 构建目标目录路径  
    dst_dir_path = os.path.join(dst_dir, year_month)  
    if not os.path.exists(dst_dir_path):  
        os.makedirs(dst_dir_path)  
      
    # 将文件移动到目标目录  
    shutil.move(src_file, dst_dir_path)
温馨提示:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:duhaomu@163.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

给TA赞赏
共{{data.count}}人
人已赞赏
编程技巧

Regex技术正则表达式\/*[\s\S]?*\/|([^:]|^)\/\/.$代码

2023-10-28 0:00:00

编程技巧

织梦教程DedeCMS系统列表页标题加页码的方法

2023-10-30 0:00:00

0 条回复 A文章作者 M管理员

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914
----《》
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索