os.path.getmtime 返回文件副本的不同值

问题描述

我根据文件修改时间戳比较文件,以便我的代码文件修改时进行备份。然而,每次我运行代码时,它都想制作另一个副本,尽管文件没有被修改。我使用了 shutil.copy2 所以元数据也应该被复制。通过输出,我可以看到时间戳不相等。它实际上还有一位小数。我不明白为什么...

我的代码

def checkEdited(old,new):
    print(old + ' - ' + new)

    if old == '':                               # <- This is an option for usage of a loop to detect the latest
        return True                             # version among many versions of a file where the first in the loop has
    oldTimestamp = os.path.getmtime(old)        # no item to be compared to.
    newTimestamp = os.path.getmtime(new)
    print(str(oldTimestamp) + ' - ' + str(newTimestamp))
    print(time.ctime(oldTimestamp) + ' - ' + time.ctime(newTimestamp))
    # print(str(oldTimestamp < newTimestamp))
    if oldTimestamp < newTimestamp:
        print('true')
        return True
    else:
        print('false')
        return False


newestVersion = ''
for j in oldVersions:
    if checkEdited(newestVersion,j):
        newestVersion = j
print('newest: ' + newestVersion)
print('wil be campared to: ' + newFile)
if checkEdited(newestVersion,newFile):  # Let's see if the file has had any changes since the last backup!
    print('edited')
else:
    print('not edited')

我对其中一个文件输出

C:\...\.idea\inspectionProfiles\profiles_settings.xml - C:\...\.idea\inspectionProfiles\profiles_settings.xml
1612983638.741892 - 1612983638.741892
Wed Feb 10 22:00:38 2021 - Wed Feb 10 22:00:38 2021
false
newest: C:\....idea\inspectionProfiles\profiles_settings.xml
wil be campared to: D:\...\.idea\inspectionProfiles\profiles_settings.xml
C:\...\.idea\inspectionProfiles\profiles_settings.xml - D:\...\.idea\inspectionProfiles\profiles_settings.xml
1612983638.741892 - 1612983638.7418923                     <-- Right here
Wed Feb 10 22:00:38 2021 - Wed Feb 10 22:00:38 2021
true
edited

抱歉路途太长。我认为类型可能是相关的。

提前致谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)