% T' n3 \( `; O$ ^( c h 2 m4 @0 e+ \" P- |7 q) b4 Idef replace(file, search_for, replace_with):' u) M& a H& ?! {% t
+ u' z5 s, u* e1 S- a( |( i
# replace strings in a text file$ Y3 h! }; `, Y, `8 u
$ G7 p+ @! b* Y' s
: X( |: [ A" {9 o
8 a& Y" K% }" T7 E# B G
back = os.path.splitext(file)[0] + ".bak" / d9 ~( o, j, [, s/ X- R0 |+ v. _ z, b( Y, U
temp = os.path.splitext(file)[0] + ".tmp"- N3 J1 u# ?( w
" \) _0 E3 J2 y$ d& F9 Z2 h5 Q
% }" v; t; r) f4 l/ m
" I% a# J8 X [7 M, U! Y
try:* h l8 }2 g$ ?& `$ k
; H) q/ Q9 e, m; u0 G% g: {# remove old temp file, if any 0 B5 [; j! ?' G3 X: F0 m3 u 7 T I3 X D/ J7 g. _) d7 a& Sos.remove(temp)$ Q9 y% D8 p% S
, o6 ], N3 M' r* G! Y( R9 kexcept os.error:5 e$ `& V# [7 Q0 f6 b
2 |! ?7 v: {+ t0 r- J+ J4 T
pass3 n0 s% \2 f6 m. F& g0 d) e
% ?- m8 |5 g8 h& G$ h6 y! L
. s3 `4 l& H6 p' X. Y
0 O0 R- b& B: L+ S. M8 u5 j6 x _& O. dfi = open(file) # 8 G# t5 a/ d( f " m3 I0 o( D2 q! w# d; A. pfo = open(temp, "w") #9 F6 G$ X; v4 \5 G: n6 k/ x" a
|: |0 h+ \ N/ Q ; V( Q% w! t- C5 c
# N6 l+ V8 l$ [- `7 s. c$ q9 Cfor s in fi.readlines():# f( `2 A$ f( R& Y1 X: G
. D: b: ^# K6 v# \# l3 @/ |. Efo.write(string.replace(s, search_for, replace_with)) & q: @! R& u% g k# {* o" @, i0 C! l# i
$ H2 T! ]; ?* i! b: c
0 Z$ l' Y ] y" g. ^
fi.close()2 U/ M- e! c3 t2 v. z8 v/ t$ z8 D4 N0 C
5 O/ i! ]0 x: T3 d
fo.close() " T. z$ ^$ B0 k, [/ R; W: ?+ j+ S: N
3 L# L- |, a8 v$ n8 C) [/ L
) e$ h f8 J$ E0 U% Q
try: 4 a9 M, {; T. j @2 O' Q s, Q5 V9 p# remove old backup file, if any( l. h% J( c8 X; n* f
+ F0 R9 Q3 L3 q; z
os.remove(back) ! C/ ^+ I1 t P7 X3 o5 t& j! D ( ?7 I' J& N* C/ J, t7 qexcept os.error: & m) d2 [6 f: o9 r" b 8 c9 v9 q+ \$ E( h% O. Jpass% h! U, P9 q# U$ b0 `5 ^
! W3 J8 K {0 k, G' ^" v8 P& z " D# g( i# Y: S9 |9 |6 g' I
, ] ], i% U( R4 M# rename original to backup... ' b/ w( N# H0 T: ?; m 3 z8 e s4 ^; f' O/ fos.rename(file, back)% O0 H3 w$ @% x$ N% h- M/ h {
$ l% X" k/ J4 Q3 u; S/ x . S* K/ c$ `& H( |+ C8 N5 X+ d0 n : ^3 y% C3 x% g3 B1 d+ z# ...and temporary to original. c ?) J# c6 r4 t3 O/ c% t
' Q) R: x* S% c; r9 {0 o) f5 P
os.rename(temp, file) * f1 [+ j8 `8 s. Z7 H+ z: W$ w5 S0 ^) D% d; W, c
) {$ k8 x! z( B& O$ o% t u' q# ^
# try it out! & K) _" G0 w) n# H$ q# F + V- v' P2 M, T! l. n. \4 B % e6 _1 O# s, V2 A
4 S3 K1 ?6 l$ y$ p" H" D' v8 v! m
file = "c:\samples\sample.txt", k) h' v' D. E5 H4 M6 c8 W. u
1 Q$ D+ w/ _' Q N + b( I% Y% T* w9 M& [. g" J4 [- \8 f$ E' I; l2 n
replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena' j( K% B' E( A! g, t! z
5 {) U, c% P, T) G0 w4 u4 dreplace(file, "tjena", "hello"); o; [6 X3 C/ @) u ^. Q
J+ _. X3 Y/ x+ R7 D
: \, l6 {: X7 o$ L& |4 \1 O. _ ' `1 G. w P$ L3 _5 g 5 {! W4 {7 q: g/ Q7 w8 q' yfp = open(file)# ]( L9 S+ ]% y" V1 ^. W
2 U: ]7 d$ p+ s3 v, T5 ^
st = os.fstat(fp.fileno())' F2 ?: q, M5 }2 k7 t1 i" g
! K: e4 B- R( G( b3 x
print "fstat", file 8 u1 f6 c. t( R, r5 `. D9 n9 R ) g$ }4 ]- P- I5 Udump(st)/ E) D+ b G( p' Z
) [, g' t, f7 I1 R: ]9 H; R 3 C* X/ q7 Q4 R/ t( X* B6 P$ y! f/ E- y) R7 l* N' n
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): `- E: m+ h. L' b1 t0 c) N; s( ^' [' o* s
os.fstat(path/file) 7 M. y( g! T1 b3 C1 [, z0 r( y4 u% }6 V! P4 r
Return status for file descriptor fd, like stat().