: j, i% {+ {& f. d9 O+ \" _/ h. o+ ] " f# s0 G i6 e$ n% t; q4 Yreplace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena+ H* [. \, g5 P5 B
; N6 Z% g) [ o" L3 }: mreplace(file, "tjena", "hello") % P1 Q$ z; y' B 1 W+ U3 [* q& K& O+ T : R$ O9 _- U4 A- u; d9 N; Q n! R1 D i4 A, @2. 目录操作9 f \+ E9 q7 {7 U0 c# c
- f/ Q9 q9 t; Xos 模块包含了许多对目录操作的函数 7 F' f6 Z1 w p6 c: C0 P ! Z- B1 |/ \' `) P9 Qlistdir 函数返回给定目录下的所有文件(包括目录)! l1 ^% a# m8 J! V) ?
. @8 J/ H/ ^' K5 M % I1 p3 U3 Q$ H7 v; W$ {5 P3 G+ E I I$ A [4 j4 g( J2 oimport os . a! ~+ P; D& f/ X; X0 |( I% q' Q( K( j
for file in os.listdir("c:\qtest"):9 _* I, Q/ l6 J, Y. p! D
2 W& U! r. \1 jprint file 6 z$ c l1 } z) U7 H* ~ # V: H1 [/ t X5 a" y) L , T$ B% \8 H! T+ y
# j4 k5 L$ I3 f8 i
getdir 获取当前目录# T( K" I% n7 e, ?: f) C
/ h/ J% l1 a1 @2 |2 x
chdir 改变当前路径8 N& T; V9 [6 T9 f
+ J/ \' L# z) H% E( l7 k1 q9 r ' j* @; w' @% f, K: j" i, C1 V* v
0 U7 T% C- p) S% D) p
cwd = os.getcwd()7 O0 d0 \2 O$ E
1 B8 V* j' h$ M3 B+ V
print "1", cwd : L8 O1 R1 g( [6 ~1 X/ d" W# R' W9 k- O: K4 W7 \
# go down1 R0 E8 @2 d* i1 w) B8 R* i
7 s# i5 W% e' c+ aos.chdir("c:\qtest") ! D" |" M3 M$ \' l* |9 p: }1 E9 q. E1 |& t* z- A
print "2", os.getcwd() , V/ r1 p/ m6 r D/ w( I8 d5 q# go back up " d+ q+ |! a1 p5 K g! |7 ]+ [- k* W6 {8 a/ P( F* t
os.chdir(os.pardir)#返回当前目录的父目录 & I9 H: P, N& B8 @# ~8 A8 r+ k( _, K
print "3", os.getcwd() ! n! y) V9 o! {& e" `" C! [ ' y+ a) x+ ^3 x+ _1 p" R - T9 s7 M, S7 ^9 c% ? I1 F# y. d0 B5 O
makedirs removedirs 生成和删除目录 - T7 _( G( K( e0 x3 [$ W) n9 D" `! T. ^
makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除/ @- { e; {. m3 ^5 _
. |( D: r8 S' }( S0 g2 S: [ / u* X8 f2 r! `) W" b- W- n0 [3 e. h$ ]- f$ P# _
import os4 J$ N" m. b+ i
( H% s" {: J) Jos.makedirs("c:\\test\\multiple\\levels") 2 L* L2 L) r& \6 V; A: O" q" x
fp = open("c:\\test\\multiple\\levels\\file.txt", "w")/ c1 n* H" ^1 {+ e! L
& N2 E7 E8 v9 @! ]+ i% M( }# F- A
fp.write("inspector praline") , k* V2 k8 m6 k" w/ v7 ~' c/ s# S. A v" }
fp.close() . d' c9 z7 m) A u) L* |; O# @6 _, Z7 Q6 A1 @8 k
# remove the file; K0 e0 @5 i# a% t6 |) w4 y
$ y7 R7 w K3 P" h' n2 d( R
os.remove("c:\\test\\multiple\\levels\\file.txt")1 z+ R8 q- @( c4 p1 X# k
4 }8 G) j5 v5 g0 ~% Y4 D# and all empty directories above it2 x% \: R+ a' P; \. M+ p
+ ^ H! {8 E5 sos.removedirs("c:\\test\\multiple\\levels") 7 }2 }1 P3 I' _6 Y2 z5 t ( y/ F. ]" O/ x$ i. q- i. D - n' Y' Z8 J. t- X) @* X
8 {. \; C& s( Emkdir 和 rmdir只能处理单级目录操作. ' D G' [# Y6 i2 l4 J8 W& O$ l/ V6 \2 I$ n
若要删除非空目录, 可使用 shutil模块中的rmtree函数 " K8 v2 d' @. r4 ~, t) {! ?0 E2 _" O o% T8 h
" [8 h) U' e( n4 y! f6 @7 c6 R E
3. 文件属性的操作 & w7 G# R3 v* X" U/ k* ^: I, p % k4 p8 V1 ~- _; M: q/ eimport os% D' r1 P( ~6 |, D, C
9 S. y$ A! A3 g, b; Aimport time! O0 ~6 b4 U% J2 I* v
1 S/ R0 j: [2 c* P e; Lfile = 'c:\qtest\editor.pyc' - I, B9 x* ]% i7 e! J. t- e4 h2 y1 T* l/ S5 K) f3 r0 I! E* C
6 p* m# p( `& [! E
+ K" _- }: M$ p) b1 {* ]! rprint "- last modified:", time.ctime(mtime)) @+ _* B" V/ M7 I* g$ v
+ y3 _. |: i# R* E2 ?/ l9 V9 y! a
print "- mode:", oct(mode) 7 j2 Z/ i# ~8 W1 R6 I) |9 N: }' y/ V
print "- inode/dev:", ino, dev % o% \4 r$ j/ S4 u! D ~- G0 Q: A$ E
9 W8 l; w9 A: v) a % a( e' p, f7 Gprint dir(st)+ c2 y1 L) t L- O3 D0 T
+ p9 a) h0 r) e) s- E3 w; fprint : A/ D; @) o+ F
/ @5 E/ ]5 O- l, l, q
dump(st)' Y% [( n7 W9 y: m2 w/ r
: n( L$ A* f2 r U7 I( j
# print " ]. J, S/ e Y, ^! ?$ i 5 | d3 R8 h% G: l' E $ q8 G2 w7 K' ~' b+ o
, b0 I' }2 p7 [: ]9 I# ofp = open(file) 1 [( _; H f. A, d3 t! P1 b* N5 B; G) } ~0 L( l( Y( O% \, K
st = os.fstat(fp.fileno()): \, `' Z8 A8 _! Z0 M6 L
2 ^3 `4 p" C; }* k2 }print "fstat", file . J) ^& M ?7 a, H" o2 c1 p - D+ Y! |) G) `4 V0 Bdump(st) 9 e, o0 |. A, @. a% c. l H* O: t1 T/ G) }
6 q S0 Q, m9 Y) x ( Z- F; K+ L% y6 a* M% V1 A+ u* c3 aremark: 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): 4 e H8 V( w7 k3 r3 J/ V; j. O B, x
os.fstat(path/file) ; O _1 F2 T- Q# ^5 `/ m" S/ ?' L9 `! i" u3 N3 e
Return status for file descriptor fd, like stat().