8 k8 N& ]! Q* k* K9 T# y; v 4 L% k# J5 H& Y; Wimport os 6 C) i0 G. h' F 1 L4 s8 ]* ]% m; @6 F- Q- }for file in os.listdir("c:\qtest"):# h% t. I; ?1 e* k) O% L. `% r
, l. y6 Z3 e' z* u2 X7 Dprint file. r6 u" b6 `0 }; ^
9 P% i/ v& p) V. g ' w- v; E/ I- r. W+ g$ h/ n ]% f l4 d( I6 vst = os.stat(file) & Z8 D8 B9 Z; w5 R. u6 k; K * p6 ^& ]8 }4 c _ ^+ Tprint "state", file0 M3 D& u+ d) G) \7 u8 U' X6 w6 K
& x+ o5 n0 \- k& z* f
" ?3 `" u8 o+ z w
) N' g* O2 w3 i3 }, L8 d
def dump(st): S) g. l1 U* B5 a . n* m$ ~4 A t0 y$ W# Z7 Omode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st* P, q- m7 k4 U2 f# L% u* L
' j8 c9 B: \& T
print "- size:", size, "bytes"& a4 `1 P1 ]. E, M% \- K
" @' X; x: v8 G% U7 {8 V1 q. E* Oprint "- owner:", uid, gid - N+ D f& u" }1 Z # F* ^0 ^$ P) q. l6 Aprint "- created:", time.ctime(ctime) + o' S1 P. R1 e4 } ) b! S& E8 t! m* U7 n5 u( Iprint "- last accessed:", time.ctime(atime)2 A& D+ o* G; b6 o, x
# V ?- r/ C. `! S5 V% d8 @
print "- last modified:", time.ctime(mtime)) a, ~! u6 j6 h3 S& a* k
4 h+ Y6 S2 E R; o. h# \print "- mode:", oct(mode) * Z% u3 Z, m* b3 q+ }* d% C % F9 u0 y2 D' ~8 ^6 a0 {print "- inode/dev:", ino, dev L. ~" `! L0 b* v, G, ^! x" K u6 s" z8 ~# J
$ [; {& I. P/ t* u
+ D* d* e: Z4 n
print dir(st) 8 }) b* K+ K3 k # r; V8 @% M# L5 a3 a- u' E' tprint $ v+ G7 ?, C1 d* z+ t: ~/ R# J$ j6 Y& Q
dump(st)( D: p+ [( K- y7 C0 ~' W. s8 X
# ~# J7 r/ S2 a/ a1 v+ D# print% I$ H, [. t) e' m- I
2 E! Q5 {% A1 N1 _2 Q+ `: B
3 f# H; o: d4 B& }- e4 Q
. X6 Q& n6 N# [4 x* i
fp = open(file) ' ^$ |4 H" |, `0 T7 X 8 ?" {9 ] U. g8 vst = os.fstat(fp.fileno())' N9 F5 A& ]3 H# x- _
1 u; M; z' g" D, v* H0 H3 T
print "fstat", file 9 d9 t# T& `6 D; c5 N: ^5 u, z! h; e7 {, j! n! Z
dump(st) P2 b% D4 w$ O, K5 o
1 U1 x" N! ~2 d" j4 @ 3 O! d# }. P* a: l- i
- Y4 q1 T' t1 S7 D4 c$ C0 v( Y
remark: os.stat(path/file)返回文件的对应属性值st_mode (protection bits), st_ino (inode number), st_dev (device), st_nlink (number of hard links), st_uid (user id of owner), st_gid (group id of owner), st_size (size of file, in bytes), st_atime (time of most recent access), st_mtime (time of most recent content modification), st_ctime (platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows): ! l) |" U- W/ }8 w& l7 L) E n% t/ R2 R( W% u. e5 o6 M0 wos.fstat(path/file) " _& H5 M8 Y, T $ n! a. s c% ^Return status for file descriptor fd, like stat().