Python2.4 和 2.6 在 Windows 上对 os.path.getmtime() 的行为不同

标签 python windows

在 Windows XP 上从不同的 Python 版本计算得到两个不同的修改时间。

Python2.4

C:\Copy of elisp>c:\python24\python
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.getmtime("auto-complete-emacs-lisp.el")
1251684178
>>> ^Z

Python2.6

C:\Copy of elisp>C:\Python26\python
Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.getmtime("auto-complete-emacs-lisp.el")
1251687778.0
>>>

Python2.6和Python2.4报错3600秒。

这种奇怪行为的原因是什么?

最佳答案

这是 Microsoft 在 C 标准库实现中的一个错误。 Python 2.4 过去使用 stdlib fstat 调用来获取文件信息,因此在使用 DST 的语言环境中可能会耗时一个小时。

在 Python 2.5 及更高版本中,os.stat 在 Windows 上运行时直接调用仅限 Win32 的 API 来获取文件信息,从而产生正确的输出。参见 this thread了解更多。

关于Python2.4 和 2.6 在 Windows 上对 os.path.getmtime() 的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1957866/

相关文章:

python - matplotlib 将 x 轴与自动缩放的 y 轴链接起来

python - Seaborn Pairgrid : How to share all axes for all off-diagonal plots (i. e 每个地 block 与其镜像共享轴)?

windows - 适用于 Windows 的 Lazarus 64 位可以编译为适用于 Windows 的 64 位吗?

linux - Windows shell 脚本中的 echo %ERRORLEVEL% 与 echo $?在Linux中: are there any differences in behavior?

c++ - SetWindowPos() 跨进程处理多个监视器和不同的显示比例

Python findall 字符串

python - Levenshtein 实现能够处理大字符串和向量

python - 用 python 读取 .doc 文件

Windows 身份验证、授权角色/用户 * & ?意义

windows - Windows XP 嵌入式中的 CUDA