首页 > 解决方案 > if语句和日期时间模块python

问题描述

我在一个文件夹中有两个测试文件和这段代码

def check(source):
    old_date = (datetime.datetime.now()) - (datetime.timedelta(days = 10))
    source_files=os.listdir(source)
    for file_name in source_files:
        if (datetime.datetime.strptime(time.ctime(os.path.getctime(source+file_name)), "%a %b %d %H:%M:%S %Y")) > old_date:
            print (str(datetime.datetime.strptime(time.ctime(os.path.getmtime(source+file_name)), "%a %b %d %H:%M:%S %Y") > old_date)+" copy")          
        else:
            print ("do not copy")

check("C:\Python\OldFilesCopier\Source\\")

输出是

False copy
True copy

因此,一个文件应该应用 IF 语句,而第二个文件应该应用 ELSE 语句,但在这两种情况下,我都通过了 IF。找不到原因。

标签: python

解决方案


推荐阅读