微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。