# D2 h2 ~% l# K6 C2 m2 N9 v5 E) Q' J* H8 a q$ s
# try it out!; f* a x( s6 C' O
9 e, d8 ]8 c0 m+ K8 v% H6 e. B
% V0 F) @7 w9 W8 X/ ~. E# z
# I) `- q$ x2 Q
file = "c:\samples\sample.txt" 0 b9 q: R+ v! a4 e+ Q$ m/ ^. A) t" S9 t* k; L5 s; t5 b0 c
' H7 B' M! u" K 7 C- ~. E$ t; vreplace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena# m5 x- n t5 b7 v% k
5 H6 _8 v4 E6 A; m' rreplace(file, "tjena", "hello") 8 w T. t' W& T" H! l0 Y2 h3 @1 `+ P+ t% q. e4 l. i
1 w- ]5 m+ Q8 B: c/ s. J- K4 V 0 S: j# g$ c0 v* W1 {, b2. 目录操作( ]! c% d9 C) O! O/ K1 @& j
$ c2 b6 ?$ s$ L& j4 |/ `+ ~4 z
os 模块包含了许多对目录操作的函数8 K9 Y" ~* ]" _
0 z6 _$ ?) V+ E3 ^
listdir 函数返回给定目录下的所有文件(包括目录) w* ^$ p' ?% X6 {9 {( {1 O
! T% k8 [+ }6 v3 x9 \# r$ \) U; i8 q, ] " h* F& B. d5 C- M3 W
1 M( X: `- b! |3 e
import os ' B8 q0 X$ x% x " j& f3 O; e% Z6 Yfor file in os.listdir("c:\qtest"):8 A" f, _6 M6 x4 \2 R" r' L
?9 h C) s4 Y' X1 u# q y. I `
print file % [6 z( ^; x6 p! L9 V+ Y1 f % X+ O9 K0 E5 z8 \. I 2 t9 m8 H- A# A
" W- I* \4 r2 U; Z9 C8 P
getdir 获取当前目录 " a' a2 S$ @: e; o" D! P+ |* M& e5 ~5 B J0 [# p, ^+ N
chdir 改变当前路径 8 S y5 B/ g6 S; D+ j8 _4 v b I7 b* z5 C
# Y; X& O. J8 `! l; ]0 e& S) m, w, a9 i8 B" c4 V
cwd = os.getcwd()/ S9 A3 v& i9 l! S
- y, C" O7 i/ p) x; u
print "1", cwd # k; u) `* J* @9 n5 S4 [) O0 x2 H. r* H5 J5 b
# go down+ R n& _1 {% a4 Z- ~: g
* d6 Y8 ]' e, M! B; t \os.chdir("c:\qtest") 5 C( g6 S9 P3 j4 A! E" j) S- n0 f9 r1 [+ v. \ y8 _3 y* W
print "2", os.getcwd() 1 e3 N3 |) N% J3 B4 ?5 h! Q9 ^* a( c& w( F$ a
# go back up% h- }) L& W$ y0 \7 @) C0 c
9 P0 I J6 v Qos.chdir(os.pardir)#返回当前目录的父目录 / E- `3 `& U/ [& _4 T : H( E' H. D* |6 B x! K. ^print "3", os.getcwd() / ?0 X3 d9 B- C, f- A+ r9 D. l' j) {6 j0 i) Y# n5 E, P
$ f9 Q/ `2 r5 ]- ?
9 N$ B. G; Y5 w9 L7 D: Z0 fmakedirs removedirs 生成和删除目录 9 P W3 d C! [; l+ n/ Q/ O" }$ H% D4 Y% ^( Z; {
makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除9 F& R! o' d6 ]' Q4 F
' U: l2 O e# N1 M5 u; @; n- q
& W4 I' C2 o e# F( a. d: |3 I- s) k5 G
import os+ }8 v+ K9 W- r% h4 ^- [
8 z1 V# r, s3 w4 _) c$ T, S
os.makedirs("c:\\test\\multiple\\levels") / |3 U3 k, K" ?: e1 {( f) B: a6 x: w8 Z) _1 ]% M8 [
fp = open("c:\\test\\multiple\\levels\\file.txt", "w") ( t t! H1 I0 E' E0 V O: o" I! ^! p& ]6 `4 \. @
fp.write("inspector praline")7 r' `. Y( Z" G9 D# X B Y9 `) |
2 ^+ M( S! t5 O$ `5 ]
fp.close() ) d$ K) C; A3 T+ `' t, | $ V% `/ }& a/ I6 p# remove the file0 `; R3 S+ T5 g9 s _
K2 E% {- Y V
os.remove("c:\\test\\multiple\\levels\\file.txt")# |: h, c3 g4 s% ?, E
5 {/ y, H) m8 O2 B
# and all empty directories above it- r/ g3 F$ s+ K c9 L. J2 q
- P6 S4 k6 k! }2 O) E' g" z " P; e& N$ [- i5 F' W# O6 r( Cremark: 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):1 X' D* Z9 v! v E- }9 [& s
. I: d2 `! `) c e) Fos.fstat(path/file) 3 M4 `0 [: E( j( G 8 l$ ]& j4 _! U7 Z5 wReturn status for file descriptor fd, like stat().