Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.stat() on Windows

Tags:

python

windows

What fields in os.stat() 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.

like image 640
Paul Avatar asked Mar 11 '11 16:03

Paul


People also ask

What does OS stat mean?

stat() method in Python performs stat() system call on the specified path. This method is used to get status of the specified path.

What does St_mode mean?

st_mode This field contains the file type and mode. See inode(7) for further information. st_nlink This field contains the number of hard links to the file. st_uid This field contains the user ID of the owner of the file. st_gid This field contains the ID of the group owner of the file.


2 Answers

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.

like image 98
Pi Marillion Avatar answered Oct 06 '22 14:10

Pi Marillion


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.

like image 43
gwohpq9 Avatar answered Oct 06 '22 13:10

gwohpq9