Python 2.5.1破坏了os.stat模块 [英] Python 2.5.1 broken os.stat module

查看:70
本文介绍了Python 2.5.1破坏了os.stat模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我刚刚从Python 2.4.2升级到Python 2.5.1并且发现了一些

意外行为,这似乎是os.stat模块中的一个错误。


我的操作系统是Windows XP SP2 +所有更新。


我有几个程序在所有以前的Python上完美运行

版本多年来他们现在在代码中产生不正确的结果

使用os.stat。


搜索2.5.1发布说明我发现了以下内容:


使用Win32 API实现os.stat / fstat。结果,报告了subsecond

时间戳

,删除了路径名长度的限制,并且stat

报告

WindowsError现在(而不是OSError)。


*********************

*问题概述:

*********************


审核我的代码似乎它应该仍然适用于2.5.1 os.stat

更改,但似乎并非如此。


时间戳已报告通过os.stat()不再正确,结果是

甚至不一致。


在我的第一个测试用例中,Python报告的所有3个时间戳都是比文件上的实际时间戳少1小时




在我的第二个测试用例中,由
$ b报告创建的和上次访问的时间戳$ b Python是正确的,但最后一个写时间戳比文件上的

实际时间戳小1小时。


作为第三个测试,我查看了最后在大约

21,000个文件上写时间戳。

Python确定错误的最后写入时间戳大约1581个文件。


假设没有使用os.stat()中的新返回值打印出

时间戳的以下代码中的错误然后会显示2.5.1 os.stat更改后的
有一个错误。


print''创建时间:%s''%time.strftime(''%m /%d /%Y%H:%M:%S' ',

time.localtime(file_stats [stat.ST_CTIME]))

打印''上次访问时间:%s''%time.strftime(''%m /%d /%Y%H:%M:%S'',

time.localtime(file_stats [stat.ST_ATIME]))

print''Last Write时间:%s''%time.strftime(''%m /%d /%Y%H:%M:%S'',

time.localtime(file_stats [stat.ST_MTIME] ))

*********************

*详细测试结果

*********************


为了演示我创建了问题的问题翼测试。


以下是我的测试中使用的文件及其相关的

时间戳,如dir命令所报告的那样。


01/02/2003 12:34 PM 0 broke_test

03/06/2007 05:24 PM 3,497,177 broke_test2

05/31/2007 04:35 PM 254 runtest.cmd

05/31/2007 04:31 PM 513 broke.py


名为broke_test的文件的时间戳为01 / 02/2003 12:34:56(dir确实

不显示秒数)。


runtest.cmd脚本显示创建的,最后访问过的,以及最后写的是由dir命令报告的
时间戳(并通过右键验证

点击文件并在资源管理器中选择属性。


------>> START runtest.cmd脚本<<<<

@Echo Off


echo创建由dir命令报告的TimeStamp

dir / tc%1


echo Last Access TimeStamp由dir命令报告

dir / ta%1


echo上次写入时间命令由dir命令报告

dir / tw% 1


echo Python 2.5.1时间戳信息

broke.py%1


----- - >> END runtest.cmd脚本<<<<<


broken.py脚本打印出创建的,上次访问的,最后一次写入

时间戳,因为Python看到它们。


------>> START broken.py脚本<<<<

import sys

import os

import stat

导入时间


file_name = sys。 argv [1]


file_stats = os.stat(file_name)


打印

print''文件名:%s''%(file_name)

打印

打印''创建时间:%s''%time.strftime(''%m /%d /% Y%H:%M:%S'',

time.localtime(file_stats [stat.ST_CTIME]))

打印''上次访问时间:%s' '%time.strftime( ''%m /%d /%Y%H:%M:%S'',

time.localtime(file_stats [stat.ST_ATIME]))

打印''上次写入时间:%s''%time.strftime('%m /%d /%Y%H:%M:%S'',

time.localtime(file_stats [ stat.ST_MTIME]))

打印


------>> END broke.py脚本<<<<




#在文件中测试1 broken_test




runtest broke_test


创建由dir命令报告的TimeStamp

01/02/2003 12:34 PM 0 broke_test


最后一次访问TimeStamp由dir命令报告


01/02/2003 12:34 PM 0 broke_test


Last Write TimeStamp报告者dir命令


01/02/2003 12:34 PM 0 broke_test


Python 2.5.1时间戳信息

文件名:broke_test


创建时间:01/02/2003 11:34:56 - Python结果错误小时

报告的时间是1小时

最后访问时间:01/02/2003 11:34:56 - Python结果错误时间

报告是1小时短

最后写入时间:01/02/2003 11:34:56 - Python结果错误时间

报告为1小时短




#在文件中测试2 broken_test2




runtest broke_test2


创建由dir命令报告的TimeStamp

05/31/2007 04:26 PM 3,497,177 broke_test2


dir命令报告的最后一次访问TimeStamp

05/31/2007 04:26 PM 3,497,177 broken_test2


dir命令报告的最后写入TimeStamp


03/06/2007 05:24 PM 3,497,177 broke_test2


Python 2.5.1时间戳信息


文件名:broke_test2


创建时间:2007年5月31日16:26:36 - Python结果正确

Last Access Tim e:05/31/2007 16:26:38 - Python结果正确

最后写入时间:03/06/2007 16:24:21 - Python结果错误时间
报告的时间短1小时





I just upgraded from Python 2.4.2 to Python 2.5.1 and have found some
unexpected behavior that appears to be a bug in the os.stat module.

My OS is Windows XP SP2 + all updates.

I have several programs that have worked flawlessly on all previous Python
versions for years and they are now producing incorrect results in the code
that uses os.stat.

Searching through the 2.5.1 release notes I found the following:

Use Win32 API to implement os.stat/fstat. As a result, subsecond
timestamps
are reported, the limit on path name lengths is removed, and stat
reports
WindowsError now (instead of OSError).

*********************
* Overview of the problem:
*********************

Reviewing my code it seems that it should still work with the 2.5.1 os.stat
changes however that does not appear to be the case.

Timestamps reported by os.stat() are no longer correct and the results are
not even consistent.

In my first test case ALL 3 timestamps reported by Python are 1 hour less
than the actual timestamp on the file.

In my second test case the created and last accessed timestamps reported by
Python are correct but the last write timestamp is 1 hour less than the
actual timestamp on the file.

As a third test I looked at the last write timestamps on approximately
21,000 files.
Python determined wrong last write timestamp on approximately 1581 files.

Assuming there is no error in the following code that prints out the
timestamps using the new return value from os.stat() then it would appear
that the 2.5.1 os.stat changes have a bug.

print ''Creation Time: %s'' % time.strftime(''%m/%d/%Y %H:%M:%S'',
time.localtime(file_stats[stat.ST_CTIME]))
print ''Last Access Time: %s'' % time.strftime(''%m/%d/%Y %H:%M:%S'',
time.localtime(file_stats[stat.ST_ATIME]))
print ''Last Write Time: %s'' % time.strftime(''%m/%d/%Y %H:%M:%S'',
time.localtime(file_stats[stat.ST_MTIME]))
*********************
* Detailed test results
*********************

To demonstrate the problem I have created the following test.

Here are the files that will be used in my test and their associated
timestamps as reported the the dir command.

01/02/2003 12:34 PM 0 broke_test
03/06/2007 05:24 PM 3,497,177 broke_test2
05/31/2007 04:35 PM 254 runtest.cmd
05/31/2007 04:31 PM 513 broke.py

The file named broke_test has a timestamp of 01/02/2003 12:34:56 (dir does
not show seconds).

The runtest.cmd script shows the created, last accessed, and last write
timestamps as reported by the dir command (and also verified via right
clicking on the file and selecting properties in Explorer.

------ >>START runtest.cmd script <<<<
@Echo Off

echo Create TimeStamp reported by dir command
dir /tc %1

echo Last Access TimeStamp reported by dir command
dir /ta %1

echo Last Write TimeStamp reported by dir command
dir /tw %1

echo Python 2.5.1 timestamp info
broke.py %1

------ >>END runtest.cmd script <<<<

The broke.py script prints out the created, last accessed, last write
timestamps as Python sees them.

------ >>START broke.py script <<<<
import sys
import os
import stat
import time

file_name = sys.argv[1]

file_stats = os.stat(file_name)

print
print ''File Name : %s'' % (file_name)
print
print ''Creation Time: %s'' % time.strftime(''%m/%d/%Y %H:%M:%S'',
time.localtime(file_stats[stat.ST_CTIME]))
print ''Last Access Time: %s'' % time.strftime(''%m/%d/%Y %H:%M:%S'',
time.localtime(file_stats[stat.ST_ATIME]))
print ''Last Write Time: %s'' % time.strftime(''%m/%d/%Y %H:%M:%S'',
time.localtime(file_stats[stat.ST_MTIME]))
print

------ >>END broke.py script <<<<

#
# Test 1 on file broke_test
#

runtest broke_test

Create TimeStamp reported by dir command

01/02/2003 12:34 PM 0 broke_test

Last Access TimeStamp reported by dir command

01/02/2003 12:34 PM 0 broke_test

Last Write TimeStamp reported by dir command

01/02/2003 12:34 PM 0 broke_test

Python 2.5.1 timestamp info

File Name : broke_test

Creation Time: 01/02/2003 11:34:56 -- Python results are WRONG hours
reported are 1 hour short
Last Access Time: 01/02/2003 11:34:56 -- Python results are WRONG hours
reported are 1 hour short
Last Write Time: 01/02/2003 11:34:56 -- Python results are WRONG hours
reported are 1 hour short

#
# Test 2 on file broke_test2
#

runtest broke_test2

Create TimeStamp reported by dir command

05/31/2007 04:26 PM 3,497,177 broke_test2

Last Access TimeStamp reported by dir command

05/31/2007 04:26 PM 3,497,177 broke_test2

Last Write TimeStamp reported by dir command

03/06/2007 05:24 PM 3,497,177 broke_test2

Python 2.5.1 timestamp info

File Name : broke_test2

Creation Time: 05/31/2007 16:26:36 -- Python results are correct
Last Access Time: 05/31/2007 16:26:38 -- Python results are correct
Last Write Time: 03/06/2007 16:24:21 -- Python results are WRONG hours
reported are 1 hour short






推荐答案

6月1日上午9:16,Joe Salmeri < JoeSalm ... @ hotmail.comwrote:
On Jun 1, 9:16 am, "Joe Salmeri" <JoeSalm...@hotmail.comwrote:

我刚从Python 2.4.2升级到Python 2.5.1并找到了一些

意外行为,似乎是os.stat模块中的错误。
I just upgraded from Python 2.4.2 to Python 2.5.1 and have found some
unexpected behavior that appears to be a bug in the os.stat module.



你读过这个帖子吗?

http://groups.google.com/group/comp。 ... 66283a8253cafb


我怀疑它解释了你的问题。


干杯,

Tony

Have you read this thread?

http://groups.google.com/group/comp....66283a8253cafb

I suspect that it explains your problem.

Cheers,
Tony


嗨Tony,


我仍​​然认为有问题。

我正在搜索os.stat问题所以我还没有看到那个问题。 (谢谢)


我刚刚阅读了该帖子,但似乎结论是这是Microsoft c运行时库中的一个

错误。


这就是为什么我认为还有问题:


我创建了一个文件并专门设置了创建日期,上次访问日期

和上次写入日期


01/02/2003 12:34:56


设定日期后文件上的时间我验证了以下内容:


dir / tc命令显示正确的创建日期01/02/2003 12:34

dir / ta命令显示正确的访问日期01/02/2003 12:34

dir / tw命令显示正确的写入日期01/02/2003 12:34


在Windows资源管理器中查看文件的属性还显示

所有3个日期都匹配。


当Python 2.4.2 os时.stat用于获取所有3个时间戳都是正确的


当使用Python 2.5.1 os.stat获取时所有3个时间戳都是错误的

并且他们完全关闭1小时


01/02/2003 11:34:56而不是01/02 / 2003 12:34:56


在我上面测试的情况下,我确切地知道文件上的时间戳是什么,因为我手动设置它以便所有3个时间戳都是相同的。

由于Python 2.5.1没有返回该文件的已知值

时间戳如何不是Python 2.5.1错误?


进一步测试显示结果有些不一致,很多次

创建和访问日期是正确的但是Last Write时间戳是

错了。它一般是关闭一个小时,但我已经看到它的情况下它是+ b $ b是+1小时,其他情况是-1小时。


我甚至发现python时间戳是1分钟后的情况。 (我知道关于FAT的2秒时间戳,我所有的文件系统都是NTFS)。我的b $ b刚刚发现python时间戳是02:51 PM并且

windows时间戳是02:12 PM的情况。夏令时或时区的变化不会是
让结果在39分钟后关闭? (我的时区是GMT - 5:00)。


作为一个额外的测试,我编写了以下Python程序,它将

目录名作为参数。

它检索所有目录和子目录中所有文件的所有3个日期

指定的目录名。

首先它使用Python获取日期和os.stat,然后它在文件上运行dir / tc,

dir / ta和dir / tw命令。

最后它比较返回的日期和时间通过Python对日期

以及Windows dir命令报告的时间。


如果使用Python 2.4.2运行程序,那么所有日期和时间通过Python 2.4.2返回

正确匹配Windows报告的日期和时间。


如果使用Python 2.5.1针对同一目录运行程序然后

你得到间歇性的结果。


作为一个快速测试,我在我的主目录上的目录上运行它。


Python 2.4.2所有日期在Windows dir命令报告

和Python 2.4.2报告的日期之间匹配。


Python 2.5.1以下差异(超过50%的时间是

错误)


处理的文件总数:149


配对所有3日期:70

不匹配所有3日期:79


配对创建日期:69

Did建议日期:10


配对访问日期:59

不匹配访问日期:20


匹配写日期:11

不符合写日期:68


这里是check_dates.py程序的来源。为你自己运行它

看看你的结果是什么。


import os

import stat

import sys

导入时间


如果len(sys.argv)== 1:

打印

打印''%s path_to_check''%(sys.argv [0])

打印

提高SystemExit

else:

path_to_check = sys.argv [1]

file_count = 0

file_match_count = 0

file_no_match_count = 0

create_match_count = 0

create_no_match_count = 0

access_match_count = 0

access_no_match_count = 0

write_match_count = 0

write_no_match_count = 0

for root,dirs ,os.walk中的文件(r''%s''%path_to_check):

文件中的文件:

file_count = file_count + 1

file_name = os.path.join(root,file)


create_ts_match = False

access_ts_match = False

write_ts_match = False


file_stats = os.stat(file_name)


python_create_ts = time.strftime('%m /%d /%Y%I:%M%p'',

time.localtime(file_stats [stat.ST_CTIME ]))

python_access_ts = time.strftime(''%m /%d /%Y%I:%M%p'',

time.localtime(file_stats [stat.ST_ATIME]))

python_write_ts = time.strftime(''%m /%d /%Y%I:%M%p'',

时间.localtime(file_stats [stat.ST_MTIME]))

win_create_ts = os.popen(''dir / a / tc"%s"''%

(file_name))。readlines()[5] [0:20]

win_access_ts = os.popen(''dir / a / ta"%s"''%

(file_name))。readlines()[5] [0:20]

win_write_ts = os.popen(''dir / a / tw"%s"''%

(file_name))。readlines()[5] [0:20]


如果python_create_ts == win_create_ts:cr eate_ts_match = True

如果python_access_ts == win_access_ts:access_ts_match = True

if python_write_ts == win_write_ts:write_ts_match = True


if create_ts_match和access_ts_match和write_ts_match:

#所有时间戳匹配

file_match_count = file_match_count + 1

else:

file_no_match_count = file_no_match_count + 1

打印

打印''文件名:%s''%(file_name)

打印


如果create_ts_match:

create_match_count = create_match_count + 1个

,否则:

create_no_match_count = create_no_match_count + 1个


打印''python_create_ts:%s''%(python_create_ts)

打印''win_create_ts:%s''%(win_create_ts)

打印


如果access_ts_match:

access_match_count = access_match_count + 1

else:

access_no_m atch_count = access_no_match_count + 1


print''python_access_ts:%s''%(python_access_ts)

print''win_access_ts:%s''%(win_access_ts )

打印


如果write_ts_match:

write_match_count = write_match_count + 1

else:

write_no_match_count = write_no_match_count + 1个


打印 '' python_write_ts:%s '的' %(python_write_ts)

打印 '' win_write_ts:% s''%(win_write_ts)

打印




#显示计数结果




打印''处理的文件总数:%s''%(file_count)

打印

打印''符合的全部3日期:%s''%(file_match_count)

打印''未匹配所有3个日期:%s''%(file_no_match_count)

打印

打印''匹配的创建日期:%s''%(create_match_count)

print''没有Matc h创建日期:%s''%(create_no_match_count)

打印

打印''匹配访问日期:%s''%(access_match_count)

打印''未匹配访问日期:%s''%(access_no_match_count)

打印

打印''匹配写入日期:%s''%(write_match_count )

打印''没有匹配写日期:%s''%(write_no_match_count)

打印


>
" Tony Meyer" < to ******** @ gmail.comwrote in message

news:11 ********************* @ x35g2000prf.googlegro ups.com ...
Hi Tony,

I still believe there is a problem.

I was searching for os.stat problems so I hadn''t seen that one yet. (THANKS)

I just read that thread but it seems that the conclusion was that this was a
bug in a Microsoft c runtime library.

Here''s why I think there is still a problem:

I created a file and specifically set the created date, last accessed date
and last write date to

01/02/2003 12:34:56

After setting the date and time on the file I verified the following:

The dir /tc command shows the correct Create date of 01/02/2003 12:34
The dir /ta command shows the correct Access date of 01/02/2003 12:34
The dir /tw command shows the correct Write date of 01/02/2003 12:34

Looking at the properties of the file in Windows Explorer also shows that
all 3 dates match.

When Python 2.4.2 os.stat is used to get those timestamps all 3 are CORRECT

When Python 2.5.1 os.stat is used to get those timestamps all 3 are WRONG
and they are off by exact 1 hour

01/02/2003 11:34:56 instead of 01/02/2003 12:34:56

In the case of my above test I know exactly what the timestamp on the file
is because I manually set it so that all 3 timestamps are the same.
Since Python 2.5.1 does not return the known values for that files
timestamps how can it not be a Python 2.5.1 bug?

Further testing shows that the results are somewhat inconsistent, many times
the create and access date are correct but the Last Write timestamp is
wrong. It is generally off by one hour but I have seen situations where it
was +1 hour and other situations where it was -1 hour.

I even found situations where the python timestamp was 1 minute later. (I
know about the 2 second timestamps on FAT, all my filesystems are NTFS). I
just found a situation where the python timestamp was 02:51 PM and the
windows timestamp was 02:12 PM. DST or timezone changes are not going to
make the results be off by 39 minutes? (My timezone is GMT - 5:00).

As an additional test I wrote the following Python program which takes a
directory name as a paramater.
It retrieves all 3 dates for all files in all directories and subdirectories
for the specified directory name.
First it gets the dates using Python and os.stat, then it runs the dir /tc,
dir /ta, and dir /tw commands on the file.
Finally it compares the dates and times returned by Python against the dates
and times that the Windows dir command is reporting.

If you run the program using Python 2.4.2 then ALL dates and times returned
by Python 2.4.2 correctly match the dates and times that Windows reports.

If you run the program using Python 2.5.1 against the same directory then
you get intermittient results.

As a quick test I ran it against a directory on my home directory.

Python 2.4.2 all dates matched between what the Windows dir command reported
and what Python 2.4.2 reported.

Python 2.5.1 had the following differences (more than 50% of the time it was
wrong)

Total Files Processed : 149

Matched All 3 Dates : 70
Did NOT Match All 3 Dates: 79

Matched Create Date : 69
Did NOT Match Create Date: 10

Matched Access Date : 59
Did NOT Match Access Date: 20

Matched Write Date : 11
Did NOT Match Write Date : 68

Here''s the source for the check_dates.py program. Run it for youself and
see what your results are.

import os
import stat
import sys
import time

if len(sys.argv) == 1:
print
print ''%s path_to_check'' % (sys.argv[0])
print
raise SystemExit
else:
path_to_check = sys.argv[1]

file_count = 0

file_match_count = 0
file_no_match_count = 0

create_match_count = 0
create_no_match_count = 0

access_match_count = 0
access_no_match_count = 0

write_match_count = 0
write_no_match_count = 0

for root, dirs, files in os.walk(r''%s'' % path_to_check):
for file in files:
file_count = file_count + 1
file_name = os.path.join(root, file)

create_ts_match = False
access_ts_match = False
write_ts_match = False

file_stats = os.stat(file_name)

python_create_ts = time.strftime(''%m/%d/%Y %I:%M %p'',
time.localtime(file_stats[stat.ST_CTIME]))
python_access_ts = time.strftime(''%m/%d/%Y %I:%M %p'',
time.localtime(file_stats[stat.ST_ATIME]))
python_write_ts = time.strftime(''%m/%d/%Y %I:%M %p'',
time.localtime(file_stats[stat.ST_MTIME]))

win_create_ts = os.popen(''dir /a /tc "%s"'' %
(file_name)).readlines()[5][0:20]
win_access_ts = os.popen(''dir /a /ta "%s"'' %
(file_name)).readlines()[5][0:20]
win_write_ts = os.popen(''dir /a /tw "%s"'' %
(file_name)).readlines()[5][0:20]

if python_create_ts == win_create_ts: create_ts_match = True
if python_access_ts == win_access_ts: access_ts_match = True
if python_write_ts == win_write_ts: write_ts_match = True

if create_ts_match and access_ts_match and write_ts_match:
# All timestamps match
file_match_count = file_match_count + 1
else:
file_no_match_count = file_no_match_count + 1
print
print ''File Name : %s'' % (file_name)
print

if create_ts_match:
create_match_count = create_match_count + 1
else:
create_no_match_count = create_no_match_count + 1

print ''python_create_ts: %s'' % (python_create_ts)
print ''win_create_ts : %s'' % (win_create_ts)
print

if access_ts_match:
access_match_count = access_match_count + 1
else:
access_no_match_count = access_no_match_count + 1

print ''python_access_ts: %s'' % (python_access_ts)
print ''win_access_ts : %s'' % (win_access_ts)
print

if write_ts_match:
write_match_count = write_match_count + 1
else:
write_no_match_count = write_no_match_count + 1

print ''python_write_ts : %s'' % (python_write_ts)
print ''win_write_ts : %s'' % (win_write_ts)
print

#
# Show Count Results
#

print ''Total Files Processed : %s'' % (file_count)
print
print ''Matched All 3 Dates : %s'' % (file_match_count)
print ''Did NOT Match All 3 Dates: %s'' % (file_no_match_count)
print
print ''Matched Create Date : %s'' % (create_match_count)
print ''Did NOT Match Create Date: %s'' % (create_no_match_count)
print
print ''Matched Access Date : %s'' % (access_match_count)
print ''Did NOT Match Access Date: %s'' % (access_no_match_count)
print
print ''Matched Write Date : %s'' % (write_match_count)
print ''Did NOT Match Write Date : %s'' % (write_no_match_count)
print



"Tony Meyer" <to********@gmail.comwrote in message
news:11*********************@x35g2000prf.googlegro ups.com...

6月1日上午9:16,Joe Salmeri < JoeSalm ... @ hotmail.comwrote:
On Jun 1, 9:16 am, "Joe Salmeri" <JoeSalm...@hotmail.comwrote:

>我刚从Python 2.4.2升级到Python 2.5.1并找到了一些
意外行为似乎是os.stat模块中的错误。
>I just upgraded from Python 2.4.2 to Python 2.5.1 and have found some
unexpected behavior that appears to be a bug in the os.stat module.



你读过这个帖子了吗?

http://groups.google.com/group/comp。 ... 66283a8253cafb


我怀疑它解释了你的问题。


干杯,

Tony


Have you read this thread?

http://groups.google.com/group/comp....66283a8253cafb

I suspect that it explains your problem.

Cheers,
Tony




这篇关于Python 2.5.1破坏了os.stat模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆