开发者

os.stat() on Windows

What fields in os.stat()开发者_JS百科 are filled with dummy values on Windows?

The python doc is not clear on this. In particular, what does st_ino yield on Windows?

Can somebody run an interactive python session on Windows and let me know? I don't own a Windows machine so I can't do it.


st_ino, st_dev, st_nlink, st_uid, and st_gid are dummy variables on Windows 7 SP1 through Python 2.7.11:

import os; os.stat('Desktop\test.txt')
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid=0, st_size=293L, st_atime=1448376581L, st_mtime=1451782006L, st_ctime=1448376581L)

However, they appear to be filled with meaningful values in Windows 7 SP1 as of Python 3.5.1:

import os; os.stat('Desktop\test.txt')
os.stat_result(st_mode=33206, st_ino=17732923532870243, st_dev=2289627604, st_nlink=2, st_uid=0, st_gid=0, st_size=293, st_atime=1448376581, st_mtime=1451782006, st_ctime=1448376581)

The Python docs on this topic would lead a sane user to avoid ever using os.stat in Windows, since there's no guarantee that any field will always/ever be accurate. In practice, it looks like st_size, st_atime, st_mtime, and st_ctime are usually if not always accurate. The other fields depend on at least the Python version, probably also the Windows version, and possibly other factors.


In Python 3.3.4

>>> os.stat('.')
nt.stat_result(st_mode=16895, st_ino=1407374883604316, st_dev=0, st_nlink=1, st_uid=0,
st_gid=0, st_size=4096, st_atime=1392476826, st_mtime=1392476826, st_ctime=1392374365)

Different from older versions st_ino is implemented.


Here's a test run:

C:\WINDOWS>echo test > test.txt

C:\WINDOWS>python
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.stat('test.txt')
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=
0, st_size=7L, st_atime=1299861919L, st_mtime=1299861919L, st_ctime=1299861919L)

>>>


Python 3.1.2 says:

>>> os.stat("C:\\autoexec.bat")
nt.stat_result(st_mode=33279, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0,
st_size=0, st_atime=1150614982, st_mtime=1150614982, st_ctime=1150614982)


Python 3:

>>> os.stat( r'C:\Users\poke\Desktop\test.txt' )
nt.stat_result(st_mode=33206, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=252, st_atime=1299861949, st_mtime=1298245084, st_ctime=1299861949)

Anything more you need?


I ran os.stat in python 3.4.

Here is code I used

import os


myPath = os.path.expanduser("~")
os.chdir(myPath)

files = os.listdir()

for file in files:
    info = os.stat(file)
    print ("{0:>20} {1:>8}".format(file, info.st_size))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜