Windows 上的 os.stat()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5275731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
os.stat() on Windows
提问by Paul
What fields in os.stat()
are filled with dummy values on Windows?
os.stat()
Windows 中的哪些字段填充了虚拟值?
The python doc is not clear on this. In particular, what does st_ino
yield on Windows?
python doc对此不清楚。特别是,st_ino
在 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.
有人可以在 Windows 上运行交互式 python 会话并告诉我吗?我没有 Windows 机器,所以我做不到。
采纳答案by Frédéric Hamidi
Python 3.1.2 says:
Python 3.1.2 说:
>>> 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)
回答by Pi Marillion
st_ino
, st_dev
, st_nlink
, st_uid
, and st_gid
are dummy variables on Windows 7 SP1 through Python 2.7.11:
st_ino
、st_dev
、st_nlink
、st_uid
和st_gid
是 Windows 7 SP1 到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:
但是,从Python 3.5.1 开始,它们似乎在 Windows 7 SP1 中填充了有意义的值:
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 anyfield 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.
关于这个主题的 Python 文档会引导一个理智的用户避免os.stat
在 Windows 中使用,因为不能保证任何字段总是/永远是准确的。在实践中,它看起来像st_size
, st_atime
, st_mtime
, 并且st_ctime
通常是准确的。其他字段至少取决于 Python 版本,可能还取决于 Windows 版本,以及其他可能的因素。
回答by gwohpq9
In Python 3.3.4
在 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.
实现了不同于旧版本st_ino
。
回答by Jason S
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)
>>>
回答by poke
Python 3:
蟒蛇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?
你还需要什么吗?
回答by ranjit rajput mallehan
I ran os.stat
in python 3.4.
我os.stat
在 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))