简体   繁体   中英

Directory Stats in Python

How does one get statistics for a directory (not a file) in python?

For instance, I need to know when a directory was created. Seems that os.stat should be the solution, but os.stat does not work for directories. It only works on files, as far as I can tell.

Thanks!

it works on directories

>>> import os
>>> os.stat("/")
(16877, 2L, 2049L, 25, 0, 0, 4096L, 1302891913, 1302891902, 1302891902)

Seems to work well for me:

>>> import os
>>> os.stat("/etc/")
posix.stat_result(st_mode=16877, st_ino=204001, st_dev=2051L, st_nlink=170, st_uid=0, st_gid=0, st_size=12288, st_atime=1303368927, st_mtime=1304030174, st_ctime=1304030174)
>>> os.stat("/etc")
posix.stat_result(st_mode=16877, st_ino=204001, st_dev=2051L, st_nlink=170, st_uid=0, st_gid=0, st_size=12288, st_atime=1303368927, st_mtime=1304030174, st_ctime=1304030174)
>>> 

Checking against another tool:

$ stat /etc
  File: `/etc'
  Size: 12288       Blocks: 24         IO Block: 4096   directory
Device: 803h/2051d  Inode: 204001      Links: 170
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2011-04-20 23:55:27.000000000 -0700
Modify: 2011-04-28 15:36:14.000000000 -0700
Change: 2011-04-28 15:36:14.000000000 -0700

In what way doesn't it work well for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM