7 ?7 j2 {4 _0 |6 K6 ]: D3. 文件属性的操作 5 [9 z4 |; r; e2 G& |6 ?) Z2 L. V6 r& t7 p% t
import os ' B3 V# s& o( E& }3 _* T& Y( H' G, g7 z
import time 5 j& E6 S P% c% F 4 ]9 Y3 Y' z5 n, k" ifile = 'c:\qtest\editor.pyc' , ^" d" Z- J l! ] * B. K( n+ J4 R, N# b 7 B* a+ s& p4 I% t $ S& l7 t( W& T2 ]5 _6 { Y: }st = os.stat(file)9 o* c+ k: m9 m F! \0 q2 C
: C" d: Q* E/ b' e
print "state", file+ u5 _4 Z/ f* m7 s0 M- O' e
2 r" r r& T9 q0 T4 T/ N# Y" f K / p3 K+ f5 J# G j: J" C' q
9 R5 A2 J& I7 P% E Xdef dump(st): / D( C& T' y2 { Q, c + u& O; \0 ?* R* l9 c8 z# w0 pmode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st ) q6 y3 ?: y1 U7 I' k9 A( x' ?9 c* N |
print "- size:", size, "bytes" & x8 Q1 a, n" F- C% H3 U : G. }* r% O' a2 \. Tprint "- owner:", uid, gid 3 ~. v# _2 H \) i0 ~ ; o* a7 x9 Q: p; a1 w$ Hprint "- created:", time.ctime(ctime) 9 c s9 c+ m" R9 K' `$ L 0 o, Q2 I$ }# O1 P3 {4 x, Nprint "- last accessed:", time.ctime(atime) 2 w. N. e$ D* R1 _/ Y4 F( O& _; f5 F) R8 U& u$ E
print "- last modified:", time.ctime(mtime) 0 ~1 }: f3 r) M" o% m" Z! k7 r, T' l: K8 ?& X
print "- mode:", oct(mode)4 X' V( k Y/ R1 P/ v, B
( D3 u8 P9 H) Hprint "- inode/dev:", ino, dev ( t4 Y' p7 T- H! T" v( N7 e' ? U1 r h- c
# M+ ^5 @0 F+ r0 ~
& @4 m. w8 X7 Q* [4 Zprint dir(st) 3 X6 Y$ Q# b: ]! k' v, Q# Q ?6 n8 \, n2 e! xprint : [' R9 h. _; Q% O0 x / y, O; H$ e* U& L$ H' c: T+ }4 X7 U# ~dump(st) 2 z+ w# ?; j J- ^1 |+ t e- T3 }6 v6 v# print 9 _8 U7 Z# [4 i 7 k9 F% u7 o) q$ o( j % ] z8 B2 g7 l9 m3 q5 S, I, W" S. ^4 o5 l
fp = open(file) 6 l: |7 L8 P- X+ K g! G% m # {4 j; _ S$ U6 Jst = os.fstat(fp.fileno()) ! P4 T. ?3 a6 q2 |9 W. D 2 y) J% d- j1 i8 f. Q/ O f1 Wprint "fstat", file 1 g: m8 k5 h, Y% k" M `# q- W0 b9 }5 g; L r4 G9 p
dump(st)4 |- j) q$ w2 U0 K \+ W5 @
- S/ x" @6 b) `$ D: |% J
& w+ e$ j, S& s- m% d ! R3 n6 d9 ?- Q7 t9 r' G$ vremark: 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): # M4 g8 @( e4 @ 1 X; F- ^# x9 g4 L; _" ~. vos.fstat(path/file)+ A7 W+ _. h/ |# c4 N- }9 y @/ \* W
8 j' f; s5 o- f4 A- \: _# `Return status for file descriptor fd, like stat(). 作者: darker50 时间: 2012-7-10 16:38
可以放进文档当成附件下载的!作者: Seawind2012 时间: 2012-7-10 19:00