# l, W; W& \; ~$ B ) T$ x( H7 Q0 U3 x @, k: H( ~
! J% X+ ~' y( A. O
for s in fi.readlines(): 6 S3 I$ e$ _3 k F+ R; B( z- \( {! G5 n% s
fo.write(string.replace(s, search_for, replace_with)) 1 D7 q, g- Y( S' G5 ^ N+ b$ R' N7 J# G) _0 ]' d ) o& p7 F( c: q 0 E( @" H+ ]2 i) W+ Lfi.close() + u8 Y4 Z5 @! V/ { : g& C3 B7 Q: z- U& Wfo.close()6 j7 \7 I$ D z/ T8 l, X j0 g
9 `9 T4 D( ~' O* _- a1 L( B! n* A
5 \) b0 R$ C: \/ i2 L& ^
! x# X& K$ Z) P2 m, s* S @try:5 m' \' j" [8 H+ i
. l" O. b m3 x; f0 ~: H1 j+ n
# remove old backup file, if any6 k: D! c6 o* u; B+ A' G
3 H/ F4 O" a3 B) H
os.remove(back)# h1 ?) C4 V ^ |5 j5 u% P; D
6 I. r4 Z5 r5 n* h6 [$ K8 C Jexcept os.error:) c, V8 D1 l$ m: g+ q" e' R
; ^ Y) s2 K& ~+ j
pass8 }+ K& A* L( z! g
% E9 z# ~6 F7 y& B* @ 1 {7 }2 H3 x5 p, f& K* A* \; J& v) ^: F' [0 o( U4 I
# rename original to backup... ( s1 G( Z. F4 d7 H2 P + H( Q0 o3 e6 U% e! t+ ]8 U' g* W1 Cos.rename(file, back) 1 i- X% z. q9 V5 S$ D# O0 U3 r0 p! n. M0 u1 i# h$ b5 P1 q& i+ \
) ~2 y5 B; v7 t/ B7 x: h8 O3 j2 J. Z. f3 n$ c! r; [8 k
# ...and temporary to original' v5 F# l8 V" _5 k3 o# h9 K4 ]
" ~2 } u. r! C1 Bos.rename(temp, file) & I, u2 Z, U/ m( o) D8 d1 R+ v1 R5 n" F
3 g" s o. y5 ]5 ~2 R1 i9 q
( _+ p+ L3 `5 g$ N5 Z6 w# try it out!5 s" H9 K$ B- N
" ~& i) c6 C. w" \$ M2 w 1 o! \# @! B* d$ d9 [
* X) C7 `0 C$ z* W0 ]
file = "c:\samples\sample.txt" * |+ }$ u: a4 d 3 Y9 G! [, o- R( |5 v 2 d2 F: e' ]; w) x
3 U7 P8 O5 @4 Y
replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena9 u) I9 W0 N# E( C, i. }7 J
' E! i0 y7 r. h
replace(file, "tjena", "hello") : Q) h; Z6 K# J& X/ c+ G" m/ ]0 O% d7 I6 Q. A7 n
$ x8 C; r, `, M 4 j3 l8 ]* h2 x8 y' D. C( n. A6 T( Q( L) A
import os* ?2 s6 L, f' W
8 x! L" n. d( m1 y( k2 }
os.makedirs("c:\\test\\multiple\\levels")2 Q; D p- D8 M' E1 d0 q- c) h
/ N* m: o& r& Sfp = open("c:\\test\\multiple\\levels\\file.txt", "w")& |3 b6 N* V* z/ u. @1 Z
, x8 L9 i4 K! t- x/ Ufp.write("inspector praline")% b: e) j* H/ ~
$ p( q& A$ [5 S z4 ~* f
fp.close()7 `7 d* s. x( j- a
* r* v1 \' \" n1 A- W, R) d7 [5 {# remove the file # n O- ^; ?% H/ q# {8 N8 Q& P2 E % h' w: | r- F+ d8 l$ mos.remove("c:\\test\\multiple\\levels\\file.txt") ; H) a: @1 t) {) @+ h( Z( p* b' g7 @' Z: p; r1 T W% i, H; V
# and all empty directories above it 5 s- k8 g c$ j4 x- j+ `# I% s' w( E. ?; R" G" j
os.removedirs("c:\\test\\multiple\\levels") % b: O2 v$ V- {. Z* ]' n ) M% }' b( j4 E! \: m: J: Z + @& z8 X- f% T( p" K
. n. c( @7 s3 ~3 |: A) [
mkdir 和 rmdir只能处理单级目录操作.. R9 f, _4 X* h! o' [
" a) l: n* i0 M+ [' F+ o
若要删除非空目录, 可使用 shutil模块中的rmtree函数' H1 F4 `" V4 c$ w: P6 ~
% ]5 K' _$ ?3 d) I# L0 R$ G: r. p . a, {, t6 ]' H* C
# U/ m1 k* U" Z' E5 n+ J
3. 文件属性的操作. J) v' ^! w. O" K" ]7 `+ b
- k% q; \- d/ C/ }import os 9 g3 r8 J% \( z d% [* M - [. o l3 q5 W: B0 Ximport time ! B4 u" o: N/ F" g- v& ` 6 c0 U) c' ~! \+ l# q. }! j qfile = 'c:\qtest\editor.pyc' 8 M. }$ `' A+ }3 L1 w4 O+ g 3 N6 B& v* d5 b f7 K & ~) ?2 O6 Z' p7 J3 M, O) D! | " m1 O0 T: s% o4 q. C; qst = os.stat(file)# r9 m& O! t9 w( d9 z% C: j7 f
, G! E4 @' m& L s9 [% D
print "state", file. K' {# c* p7 y9 {; H
. ~: O6 d. c1 H9 Q
% e9 a, K# \1 G' B9 R( h
- A+ }9 y; a- _) |8 c# Cdef dump(st): $ f6 o, s. e+ o% k. g2 |9 r2 M' t! D, B. j$ Q, `, G
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st. X6 Q o+ R- z( F. B: Z
1 l. E1 C* F& m4 b% M- y6 a: t# @
print "- size:", size, "bytes"( h: T2 \$ _, n4 g
: z* o/ D. ~& V5 t , R. Y2 [$ C, l - i, W, w F+ [, T" c. hremark: 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):; z5 ?, g1 V' a3 z2 R7 k3 H
?- _2 s1 \" K4 Z# _
os.fstat(path/file)2 a6 `6 X" Q/ }* i
* q2 [5 p' ^" _Return status for file descriptor fd, like stat().