0 \0 J1 p s' ^6 g# |os.makedirs("c:\\test\\multiple\\levels"). I2 ^* |' x* P2 z8 Y
; \5 c3 X5 P0 h
fp = open("c:\\test\\multiple\\levels\\file.txt", "w")' W+ ^0 e4 _* J0 F4 o* S. D# E
. U9 `4 |. n; ~
fp.write("inspector praline") 1 ?6 [3 p! ^7 P% d ! v; {1 M6 }4 Vfp.close() ' c4 K" y# G1 ?2 G0 `0 X% P' a" A C) E6 p6 x V
# remove the file% O4 X3 Y1 Y ~, h( R& N6 P- u
! a# }3 S3 z6 K
os.remove("c:\\test\\multiple\\levels\\file.txt")* t1 O% q* U( R4 l! k ~
- i6 c N4 U. D" ~! \
# and all empty directories above it 3 f: R2 g# p8 |: b/ s; y4 |' W7 t) D5 c p9 \7 ^% w9 f7 ^
os.removedirs("c:\\test\\multiple\\levels")5 S1 l z- }( t4 B) d/ _1 w
$ A5 G2 G$ m2 U4 v# r% { 3 p4 ?) s8 |% s5 s5 A' o( ?; s1 k4 B w- @# ^
mkdir 和 rmdir只能处理单级目录操作. 5 G& s- m* ~/ n' Q, c& W/ D1 m. w D8 c
若要删除非空目录, 可使用 shutil模块中的rmtree函数8 p0 y3 X% @! M U* m% Q
0 x; r! P& M/ Q4 \, | ) Z% r+ O3 O0 I; h- x8 T
" T+ r+ }4 h8 p/ x5 \+ J3 v3. 文件属性的操作5 i8 Y- f9 K' W# U
* ~5 F1 u9 ?4 d* o- J
import os / E( b5 K! P2 Y7 O# y s8 `0 [5 q6 d' M7 o' T
import time : Z6 W5 D3 ]6 {: T) @8 X7 k; F0 P9 U& L7 W
file = 'c:\qtest\editor.pyc' . X. s4 O ` ?2 P/ M. g7 d . B2 p1 f& k8 T, s4 `- G* U 4 m$ x0 r( x v" M/ J( H r 2 p6 M }$ u( l& m1 ~st = os.stat(file) " S. p( b$ m4 l/ ?$ o) L2 Z6 @5 g! c9 V3 B' ]1 C; A T; V1 y6 D
print "state", file/ j+ z$ _" z! A& t' Y- H. A
# G% g0 ]4 @! p" T) n& W
+ H( r, ]4 k# x& {% O0 ? : a2 d+ {7 l4 p( `) Q5 A/ a0 Qdef dump(st):2 ?* J9 M) d. M0 i$ k1 {( X
/ h' V1 P" c0 M& P x
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st 8 t. i2 ^1 w. [' i" U1 E% r4 c$ x" ^, ?7 R0 o9 j+ s
print "- size:", size, "bytes"; D& ]' z, @! i" A
- h- C6 L. i0 S- c% W% h) {print "- owner:", uid, gid' Z# h- C+ Z S1 R" P$ x
2 a6 a: W. }7 ]# w; S' U5 v
print "- created:", time.ctime(ctime) # h- p( g- G% M/ r , |5 w8 h, X' q2 G' sprint "- last accessed:", time.ctime(atime)$ h8 k J# A1 ^% A6 F1 p- ~
, H- w3 M1 E$ D3 y7 h; G J$ @
print "- last modified:", time.ctime(mtime) 2 G1 K! F ~1 ~. c" h, D0 \! M3 J) f: V
print "- mode:", oct(mode)! a0 _# R7 G0 I. D% u8 O
O4 ~/ q' ]- |* P
print "- inode/dev:", ino, dev$ r$ p6 J7 G1 e8 R9 R: |' p
+ [- z6 I! s) F) o
: k! d0 f% H N4 }0 m1 l
# M" M# N3 b h# l/ g, Zprint dir(st) ! ^; ]8 w5 o# p2 i6 b a/ _8 `6 O5 f7 V7 o9 B
print ! k7 L0 w& t; M! U: w- U. M9 z6 }2 a6 O! {. Y+ t: B
dump(st)" X( Q6 k2 g [
! k& Y* Z( f- A1 A, s
# print r: t6 Q3 c5 R9 G+ [2 v
+ q( r9 E. o$ Y$ D3 s# `1 x 7 r/ {( F# O, K7 L9 [+ _
# O4 I9 N7 L1 X. [) mfp = open(file)# Y/ f7 B+ q7 |" u
" Y; @- J; f) D; P) t
st = os.fstat(fp.fileno())4 b+ H% R( A( z' R6 r
2 P+ b2 g [" l3 o/ N" k4 A2 Kprint "fstat", file ; f, M+ [! b. @( l8 K# e. Y( {. w, j4 ?7 j
dump(st) + f0 g+ Q% b# q: c1 s0 E ; X9 x7 t# D0 P+ ?; I0 ?% z1 A ) S, q0 @" k& U; p7 P 8 v1 u1 r9 K7 W; S/ z& fremark: 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):% |7 P& b7 q1 g
: P2 j4 x2 r: T" b9 m1 Z$ A) bos.fstat(path/file)6 `) j6 ~' a8 N: [
/ |. ]. P% e- N- j5 W
Return status for file descriptor fd, like stat().