【问题标题】:Check file modification date and copy it to target direction and copy only those that are modified in given date检查文件修改日期并将其复制到目标方向并仅复制在给定日期修改的那些
【发布时间】:2022-02-04 19:02:36
【问题描述】:

所以,我有这个代码

target_dir = "/mnt/..."

for source_dir in glob.iglob("/mnt/.../*bla.png"):
      file_date=(os.path.getmtime(source_dir))
      d=datetime.datetime(2022,2,3)
      if file_date>= d and file_date < (d+datetime.timedelta(days=1)):
        shutil.copy(source_dir, target_dir)

但是当我尝试运行它时,它给了我这个错误

File "a.py", line 33, in <module>
if file_date > d and file_date < (d+datetime.timedelta(days=1)):
TypeError: '>' not supported between instances of 'float' and 'datetime.datetime'

我被困住了,我不知道该怎么办。有什么建议吗?

【问题讨论】:

    标签: python python-3.x time


    【解决方案1】:

    os.path.getmtime() 返回一个浮点数,而 datetime.datetime() 返回一个 datetime 实例,这就是您收到错误的原因。一种解决方案是使用fromtimestamp 方法将浮点数转换为日期时间:

    file_date = datetime.datetime.fromtimestamp(os.path.getmtime(source_dir))
    

    【讨论】:

    • 感谢您的解释和帮助。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 2019-02-11
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    相关资源
    最近更新 更多