. B/ u7 x+ p# D8 a2 Y# remove old temp file, if any 6 W- }3 T! R: J . E% ?1 b2 f1 L% q/ _( ?os.remove(temp) % D/ F# z* C6 x% T6 v% w ( |; {9 K% ~' d4 Nexcept os.error: / ?+ ]! x6 m+ L/ l + z2 N; j- v, xpass1 `* K, l& E6 M) m) n1 E
9 O( e6 `3 A( v W9 Z; n a+ U 5 ~9 S% ?3 V8 W5 E5 u1 \
9 A2 k- Z. V% Q" H2 _
fi = open(file) # * [5 u9 N. t ]. M" _, s7 D" G, [' @' V/ R' I
fo = open(temp, "w") # : h; C" s. c4 S/ V! l" u! N: p d" y 4 d# H; P; `( I$ v4 k! u1 } , u. k. F; S5 u) `
: R! Z. @( l+ S: P4 m
for s in fi.readlines():& |5 {6 T' g' p( m! w1 J5 A! y
+ l! V C8 ?+ M, E( \, w4 P
fo.write(string.replace(s, search_for, replace_with))) G2 a N3 J* M$ U5 k5 P
% V9 j1 I, I3 e! ?( i/ g( o, S$ G$ O
# m* i: S0 {9 v% g6 J2 z- Y) I o
% I0 ?) G, v5 Q( e: \1 O7 pfi.close()2 {. K( O3 h% B* X
9 ^+ H! ]+ w3 V3 o0 A
fo.close() 0 t8 c w& U* e c' M& ?, Q( j1 `) H$ L0 O% r6 l' Q) Q1 ?# n6 e
& I1 U, J- P9 w( F* _3 Y
t1 j7 x# b4 W1 z+ itry:( e* W) f. P8 r6 Z
0 G+ B( u! M# F7 q ]# k6 a6 d# remove old backup file, if any 6 V* q: }3 O' i- |0 ^# z; V2 O& l7 T$ C$ w
os.remove(back); \% w7 {/ K* w/ @$ Y
0 y& Q$ L& j2 m( P: M F( zexcept os.error: i p* A! ~1 M; ^$ c $ ]: ]4 k( b* u) Xpass ! C5 W; ~! t _1 X ; k8 w `) Q8 h. H 1 Q' g7 B1 x: D1 e9 ^6 b' v7 Y
6 P" I% T a4 R3 K/ ~# r L; u# rename original to backup..." j- S% H! R% X* f. `& c4 }5 G
6 O! o& ]+ ]8 @3 h' Q8 m2 i4 t9 P
os.rename(file, back) + b% m8 D9 U0 l/ |4 G* p2 y* E% v: ?2 P- X1 M
; u% y c8 o- \7 N
& y' _% L5 I8 P6 Q6 c- p
# ...and temporary to original$ B" Q# z0 p6 x3 M2 `/ ?. A7 O
2 v6 Z5 |0 t. ?os.rename(temp, file)/ T* ]1 V3 T$ L# g1 Z. H
4 m8 \3 K2 K% E1 H . F* H2 ~% Z4 N& {
& _/ k- o2 F; d, Y1 `' k# try it out! $ J7 U/ A* ^/ ^- ~! }* S* E2 f/ q) {2 D; n1 [+ d
1 P2 [) r6 Q: R
4 p' o% u. A, m, T$ l9 n
file = "c:\samples\sample.txt"$ b% a: l: D" H N/ @: f! ~
# [3 O, [& X+ K, }
0 x. P# I; z: p; @- N. e V2 [
0 ?3 C; K# ^( g' ^: a+ E5 G/ T
replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena, P, K k7 a5 Z2 q. f/ N
0 O, W& N4 D( O4 B. X6 t
replace(file, "tjena", "hello")7 a) g6 K! W, b: `7 V
! z; ?! Z" E6 E# Zos.chdir("c:\qtest") . q2 \! u: c. [# P' N! f* a 3 f3 F+ i4 y" F3 M7 n2 Lprint "2", os.getcwd() ' v; D0 Q( J. V8 f1 r y, x9 G$ p+ G" b# d0 N
# go back up + F1 m. T7 E, T4 z4 h, _! d/ e. P0 r
os.chdir(os.pardir)#返回当前目录的父目录1 a% H A& J" P1 X# L. n$ m
2 X4 b9 G" I( q' o9 L8 |
print "3", os.getcwd()* T% L+ a4 S' j3 Z
# Q6 u7 }8 ~; H# h6 B% w1 \% ~
Z1 S/ H$ L! z% K' s) U$ q5 {5 Z0 {# _" w! Y
makedirs removedirs 生成和删除目录 " m6 t b' f" }5 s" R* ?% Y/ y" _3 U1 t# r* |
makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除 7 A: p$ X! D2 Z* t4 E7 r) k' M; P: `9 W1 E1 I4 ]
! P: v3 n6 r2 ` J6 R- [" E
; X2 Q1 W. n! b5 G* eimport os / H. }! ` _& F( Y- l * n. O7 ]$ W' ~8 cos.makedirs("c:\\test\\multiple\\levels") q7 U0 R u+ p; p$ F* [$ Q$ W( t/ p9 V) W" J1 e
fp = open("c:\\test\\multiple\\levels\\file.txt", "w")& R* B! i5 q3 ~, S
( V I6 b' @1 F6 H8 l2 [fp.write("inspector praline") 2 O1 T7 o, c. g7 }$ j" N7 y- A# g
fp.close()+ w: o5 `3 _+ Q: F0 L
4 k4 s1 E& `$ G) K, G3 w# I# remove the file $ n% m; i. q; |, Y. t- Y/ x, w j
os.remove("c:\\test\\multiple\\levels\\file.txt"): W ?1 q6 M! u% u. D
" a+ N) p5 a7 T. m4 L% z# and all empty directories above it . U6 E% [: t, N K( q # ~% B9 c) ~5 J, a$ c- z% Jos.removedirs("c:\\test\\multiple\\levels") ; f7 x9 m; K7 o6 ^7 Z& [7 F+ B% `% Z* x7 D4 }$ v. U) z
# V$ m$ [5 ^: v" ? w& Q% @9 \
* r0 c: z5 _5 A7 \- O) o1 p0 M6 E
mkdir 和 rmdir只能处理单级目录操作.6 b: s& M* e, O
c7 [+ q- E5 b! o1 S若要删除非空目录, 可使用 shutil模块中的rmtree函数 ) Y2 Y) _8 \ @, a7 T% z8 P8 S9 s0 p. H y
) E9 N+ J, o: E+ ?. E
$ f4 U; `- B$ @' ^
3. 文件属性的操作* M: x' x. W$ a
' t0 n$ d4 Q* W8 l7 a! z: l7 k
import os $ {9 _% @$ D8 D0 u( z4 q+ B4 ?; y' _
import time5 {& [. c6 ]7 }& A) n6 K! J! M
+ F4 K1 [. _6 |9 L8 V, @file = 'c:\qtest\editor.pyc' & M3 u( l/ A5 t2 |* {1 v! u$ T * i, v1 f2 y- P7 m3 G ; o$ ]! e; b3 n5 Q. ]
& q9 k6 e) ~% ^0 Q; l$ e3 I
st = os.stat(file) 4 H9 W0 ?( f& y1 R- {; S7 l. J1 B( Q! Q" J, @2 R8 m
print "state", file ' m# x: t9 F# b0 P* c! J) o" q# s4 K9 X
& v. D1 @! T( T( s# e3 Z6 F4 S
! y- ~' N" J2 ~0 ~# p4 sdump(st) ! J1 L0 O7 F0 G) E7 S8 J : n( Y$ E/ A' w. a$ ?! @ [' K: h1 t3 { # ^) E; n! q3 b/ F+ D+ o6 H- q; I- M z
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): ( z! ?5 t1 S: ]( ~9 o) ~. s9 M0 ]+ U$ T& E$ c. D0 X+ R5 W4 V x5 }
os.fstat(path/file)2 K' a0 I. y0 i" ?
3 h* T! t5 W- {- [5 M
Return status for file descriptor fd, like stat().