5 w, Y: [9 V( r8 s+ I# ~fp.close()3 V+ D9 Y W1 C
7 h8 O3 p, g+ D( f, e2 t
# remove the file ) t- y# d* ]' r2 R. U! { " D* @- }1 s! t" Y" ]5 Ios.remove("c:\\test\\multiple\\levels\\file.txt") 1 ^$ \+ i# D0 p. ` 0 M) k$ V, O6 l, s3 @" m# ]& I# and all empty directories above it7 J4 P) d1 X) b1 `& V6 V$ w7 `
! Z+ L& v4 M! W) X9 _6 H
os.removedirs("c:\\test\\multiple\\levels") & m! ~1 B4 A, r5 P/ X6 M) b / y' N& V" U: G! ]6 n8 Y 9 a) t% C8 i5 E8 V' N) L8 H3 c6 Q! W
1 ^% O- g6 W; P, i9 R7 g
mkdir 和 rmdir只能处理单级目录操作.7 e) c. k& `' ^/ g
9 v& M- q! S- [" p/ Y* B3 x+ q" |
若要删除非空目录, 可使用 shutil模块中的rmtree函数 0 ~- y: u9 x% y' S# X/ n+ h8 C8 ?) z/ H1 u/ U; U" J
; h! U2 E+ a8 u ^$ G% t/ K2 I% Y0 d) |6 l% m% ?; x# A
3. 文件属性的操作: Q! O" r8 r: y9 O. S
3 w8 x2 i. ?. h6 S X7 r
import os& R6 Z/ P' q% ]4 K" J
& V5 |" ~, z1 r+ b7 eimport time ) y3 h$ Y/ t( E% n - x- ?! s. y# A' G) z5 Jfile = 'c:\qtest\editor.pyc'" T* B# I* f! w+ r" Q- I
2 A+ W2 ^6 p) F' U/ J$ s/ Z5 z
& r) G% s! L+ B5 J/ I# _! M
, a( C' Q' R7 O& C) W$ a% Ast = os.stat(file) ! C: r8 }3 k9 M# t3 |, }. W ) Q3 m2 ]5 M5 I$ E" P. M, \print "state", file8 F2 B* q5 o! L5 q& Z1 z
$ l y3 v' C% ^" Q+ M1 @% p8 p 0 |+ \% o2 N6 I
; N" n' V; y' f6 z6 l
def dump(st):2 [4 {& d. Q2 P$ C0 j7 v" D0 `4 N" J }
% G# t8 Q% ] n4 c2 s
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st # @8 \) S9 n0 N6 y, E( `4 B- l+ J# [6 e$ W$ |+ L
print "- size:", size, "bytes"+ m( V* w3 u, i+ U
! d9 y A {4 A8 J) r
print "- owner:", uid, gid; T% ^) g+ l- o6 n
6 v# `' F6 W1 Y3 {: F
print "- created:", time.ctime(ctime) 9 Z+ j# b+ r. B ! v; K. G4 l5 w' O! Z0 ?" q* ]: Yprint "- last accessed:", time.ctime(atime) 0 C+ r! M9 h% ]! W7 o0 B1 v . M3 f& O% e# ~; tprint "- last modified:", time.ctime(mtime) 3 C2 C+ _9 t7 F6 y' e% D5 ~4 l" K( x
print "- mode:", oct(mode)) g. r5 A+ X4 s4 \: {8 H, U
' y) w3 b2 G4 T, a* D% w) \/ N; _
print "- inode/dev:", ino, dev& v* V$ |8 _# S" h9 b; n6 a/ K8 w
0 v4 [$ ` S3 t8 D9 `8 A & D) O+ w( r. t' `* X: q
, g2 P& m+ t( j7 d% Kprint dir(st) % e: k! x' }8 B. S! ~/ s. l- b7 W P+ l8 N3 j4 a& A
print , S2 |- ?- ?3 u, Q+ z! [ 2 j/ x, C. \; adump(st) 6 [4 x1 f. i/ \! Y 4 v2 G1 F5 k1 W2 |# print ( m4 |/ q3 w$ C. p4 o! I) T& l* p3 k0 W
5 D, s- w' e; O" v; @ @
1 t, |% c' G6 M
fp = open(file) - m% c7 ?% R0 k/ F4 L# Q4 N8 t/ Z) |
st = os.fstat(fp.fileno()) . a' a3 K- A8 g E \" n5 S$ z- \) f' A3 U
print "fstat", file " c6 M5 D# R, v4 s, u' T# @3 o* ]. W
dump(st)5 V. M h" U4 u6 ~
; _' A+ }2 T7 {4 n T / m: i$ C# V: Q5 J3 R4 S t: f7 p- x8 Z# \
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): 0 R2 W) [, q3 m7 h, O6 `9 T- j5 a8 e. Z' d4 \8 ~1 o5 g& h2 ]3 `
os.fstat(path/file) : N; Z, G) x) r% x5 _, c/ H( ]7 G6 \- U6 D; ]5 u* ]
Return status for file descriptor fd, like stat().