Problems wth os.stat().st_mtime on Mac
glassfordm at hotmail.com
Fri Sep 29 16:28:05 CEST 2006
The Python 2.5 News at
http://www.python.org/download/releases/2.5/NEWS.txt states that Python
2.5 was changed to "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)."
Although not mentioned in the Python 2.5 News, apparently there was a
similar change on Mac that I'm having some problems with. On the Mac,
just as on Windows, os.stat().st_mtime now returns a float instead of an
integer. My problem is that the decimal part of the float is
inconsistent in two ways.
1) The more serious problem (for me), is that on two machines (PowerPC
Mac Mini running Tiger and Intel Mac Mini running Tiger), the decimal
part of the returned float always appears to be ".0". On an iBook
running Panther, however, the decimal part appears to be ".0" for many
files, but for other files it contains actual significant digits. For
f = open("/tmp/tmp.tmp", "w")
#Both Mac Minis: 1159536137.0
a) Why the difference between machines?
Also, on the iBook, I created this listing:
>>> for x in os.listdir("/tmp"): os.stat(os.path.join("/tmp",
b) Why do most files on this machine have ".0", while some (generally
those I created myself using Python 2.5, I think) don't?
2) Even on the same machine, the results are different depending on how
I access them. For example, after setting up as follows:
strPath = "/tmp/test.xxx"
f = open(strPath, "w")
I get the following output:
(33188, 1822331L, 234881030L, 1, 505, 0, 0L, 1159532547,
#Note that the results are all integers, including mtime
#The result has 7 decimal places
I understand how the results can be different: the os.stat() function
returns a posix.stat_result object, which gives back an integer value
for the mtime if you call __str__ or __repr__, or if you iterate on it;
and a float if you get the st_mtime attribute. But I would consider it a
bug that the results are different: a float should be returned in either
More information about the Python-list