! Q `. B7 g0 u$ o ! \, U: R# u, P
' `; O* e7 w9 ]5 M- a% q
import os $ s; l5 X2 [5 q % _4 h# L4 B2 w( afor file in os.listdir("c:\qtest"):& x9 s4 g, l) w3 I
$ O2 z, t9 f& n9 f7 \/ a
print file" L3 O; g; D& g. X/ X7 B
+ K# o$ ]! F8 r, n
- c, d7 Z: l6 y9 g% v% v: C2 y $ U& x7 q( z. d0 W3 Igetdir 获取当前目录) H) C+ @% ?% ^( x
+ P& s+ l1 w; }) t; t" h+ H
chdir 改变当前路径 2 c" a! D1 i7 _3 F. d + I, D3 \/ d- t+ V + W( Z9 | t3 R
7 ^; ?. {/ P+ w. m; `
cwd = os.getcwd() ( r8 y" l6 G% q , j _! r8 w4 u. Wprint "1", cwd 6 q' `% H" w- a; ~" k4 c 2 G* U! _/ @3 U+ J$ D1 u# R# go down0 b% s2 p r! B. G: m
/ Y+ u& q' r5 [os.chdir("c:\qtest") ! d* I2 ^" Z8 z$ c/ o9 d. v" W+ S# ~
print "2", os.getcwd()/ C8 n* q( I% L) x
b4 v8 q4 G: v$ a4 M1 }4 j: I! l2 p
# go back up 4 E1 H" `, q" R, g, n+ _% | ' F/ _- l% z# M3 L1 tos.chdir(os.pardir)#返回当前目录的父目录+ z) x" A" ^8 n5 W S5 Y+ p: [
5 O; U+ _/ m/ h/ B$ I1 {3 @4 l
print "3", os.getcwd() & d: U# E7 T; L* a, K. z 8 O+ E; p4 ]3 B7 U 4 C7 V% y7 V" H7 Z* w# F* e' s % Q4 [" A9 z" Jmakedirs removedirs 生成和删除目录; E) U# }8 K2 i. l+ z' A. i0 O7 J
* y! T1 \; { T! P3 x3 ^makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除* s# _9 R+ j; p; C
R+ e3 s' r; Y4 F
* Z1 ~3 O6 ?- e+ r( d; t' T/ @; { & Y7 N7 h3 l1 \3 f. k, d/ h' mimport os8 d3 O+ d1 B1 {3 r) s% a/ {7 h# ]- l
1 D0 M% w, Z* n- s& \( H
os.makedirs("c:\\test\\multiple\\levels")9 C) ]& G+ v0 g$ [$ Y
+ K! @1 e. M# ^, S* ^- O
fp = open("c:\\test\\multiple\\levels\\file.txt", "w")5 B1 |8 s+ q& Y A6 Z
0 l+ S) H& c9 x# ?fp.write("inspector praline") " ]' d4 Z) J* M" J$ h+ T : V% W5 B1 K* I& xfp.close() ' h; r/ A9 N$ K* c, \# Y8 ^, n5 c3 `! o0 M. W1 O" g/ n7 k3 a
# remove the file ) ~9 U, O J/ x4 D1 ?$ N0 ^# C" {- x5 p/ @+ e' l4 t: }$ K
os.remove("c:\\test\\multiple\\levels\\file.txt")1 [8 F1 V$ M( ?3 T
; B Y1 b4 H4 K- K. j# and all empty directories above it # b4 n) _( c X0 b3 X2 M$ `/ K! u7 R$ a0 Y
os.removedirs("c:\\test\\multiple\\levels")4 R5 @9 K1 k- Q
0 A2 U; Z! C; Y# r5 `/ W ; H6 w. \" q3 r& m. P, \& P3 A2 Y' C4 {# Z# ^* V: m7 L7 q$ g7 _/ G
mkdir 和 rmdir只能处理单级目录操作.' P7 u S( B* I
; ]! x. P. V" }5 d( B3 W若要删除非空目录, 可使用 shutil模块中的rmtree函数$ T5 J8 T: j: ^7 y: x
' t. O/ t# B1 Z3 R, y: V ) n0 x, h: u) W! C' j / ~) x% A- E( V" e7 d3 h) a1 d% k3. 文件属性的操作& q4 p" @2 Y3 R' X, M" H
6 z/ @* I! h* t# O' i4 z0 b
import os. Z. a+ Z* s2 m5 {4 @
2 ?. d* {) `' I9 ^- q. Bimport time % B3 J% G Q! Q! M$ E& u* d. G& X6 C7 d C" ?
file = 'c:\qtest\editor.pyc' & W% H" V, A6 [# E/ a7 Y* p5 \- i# Q9 N. P/ k
4 `0 N' L. j2 E ) e% ]9 s; H$ O' lst = os.stat(file)& q9 c3 p. _8 k# Q$ j4 y5 n
! L9 l! B# b3 W1 rprint "state", file 0 Z4 [3 |/ W6 r8 I3 J6 N/ ]# |9 `6 H
+ q/ N7 X: W+ r
. D' r4 |* k/ R. M" H; b
def dump(st):" w4 V6 b! e9 d4 x! V, T& ?
! d2 ?2 p0 R0 s% R& L
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st6 R) K5 [! E' A7 g1 y; R
- b2 V- S, J/ x3 j* r* p
print "- size:", size, "bytes" 2 S, T& s) @; x+ N4 R% q9 e1 `& V* P
print "- owner:", uid, gid) k. ?; l! B8 A
5 _- J+ n& \. S. g# i b7 A- fprint "- created:", time.ctime(ctime)" U( Q; F, W9 M. x
: o1 \# e9 Q8 [2 a$ v7 x6 _
print "- last accessed:", time.ctime(atime)9 R& J9 H y/ w1 ]
, ~7 X- Q) F0 p1 a& o# Y0 `) iprint "- last modified:", time.ctime(mtime) 0 p: v- \# V- n' E5 D8 h3 V/ `& F3 q9 {- E. ~, E
print "- mode:", oct(mode)& N. Z5 z6 m" W
* V9 N& k- c4 _5 z5 a( U3 J+ q . ~8 Y1 ^9 i9 Q! f' c 5 k9 F. j7 O$ A$ f7 [+ q; Bremark: 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- R0 B: r+ ~, ~$ j7 w! d
! p% A! t# r+ _3 H! B0 f8 b. tos.fstat(path/file)1 Z( _" i5 R4 P( B g" l, y
/ w2 x/ Y! W8 g" j# E. ^Return status for file descriptor fd, like stat().