% @7 A: |# S- sos 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作; U5 z, F9 b" p. ?& R
1 Q. s0 q, N7 G* z# w5 ~ 4 F' F9 r J' x' O+ C- k # t/ ]# K4 Z+ Y* N* p" M7 e7 A; B1.文件操作 ) O2 u" U6 q" G& N; x; V ; X& w, K' a: ]& Cbuild-in函数 open 实现文件创建, 打开, 修改文件的操作 " d$ B3 G' ~! a2 O* `7 n0 _0 M * U+ G6 p4 }9 `1 j3 E1 O$ y * X/ E K" ^: d$ ~ 7 }5 V6 R% X2 t" C' Q3 y6 kimport os 8 | R- q1 `+ I& |4 W" c0 _$ L1 J' o- z8 i% a4 S X9 P; H
import string 7 S/ o0 k4 A' s+ j8 [3 ~, a7 g$ X2 L1 ?1 b' B+ j6 u5 K, b
4 [# C5 o: {" \# ] s5 w 9 Z1 C4 V4 R- L7 S! v( x( T9 ^def replace(file, search_for, replace_with): 2 x+ D1 n) T @; j. b; Z% Z7 R0 d: H" x ^! [
# replace strings in a text file 4 D G' Y0 F3 o& R O, @9 B 8 }) I! U2 q( U. T& A M5 L4 N$ P; b& p. k8 k' R9 }1 i5 I
8 M9 y0 [2 M4 n1 n
back = os.path.splitext(file)[0] + ".bak") A& r' A0 z6 T
! u3 i8 w9 ~" Z7 Atemp = os.path.splitext(file)[0] + ".tmp" 0 T6 ^1 K$ b6 b ) y0 z5 u) k% @/ P; |5 A3 Q( R % b5 b o2 v9 a9 u' o2 N$ r; ?0 o4 R0 N! K2 f; t
try:1 \, h$ Y; F+ v) c
1 |) t3 w1 s/ k
# remove old temp file, if any7 [+ v7 n. r0 R) F U F1 z
1 O z4 H8 L4 G1 A
os.remove(temp). s8 a1 R" [5 A3 N! o1 }6 G ?
7 {8 F5 ?7 ^; B
except os.error:, k8 D$ F5 j$ H+ f3 t2 t
( z# {9 ^7 ^- X) i7 g
pass # U# ?5 c7 Q/ T/ G4 @7 q9 J) V, m8 o7 z) Z$ H {" c/ t
4 }7 C+ z) G# g4 C7 y
- d8 E8 R5 s- X C d; d/ J3 L/ t% g
fi = open(file) #- R9 J- e U3 z2 U
^- k$ o4 Z4 g* D6 g- |' v! z Lfo = open(temp, "w") # 6 k( C& A. o' @' f6 V, @2 C! _; y8 ]# _: U. c- [- x
1 y/ c5 s; z1 P. X6 B. w/ Y+ E) p) Z: Z! o u# D
for s in fi.readlines():1 M2 Q/ B! @- J- u4 K- ?- Y
% a* s: K1 a, dfo.write(string.replace(s, search_for, replace_with))" C, Z; J+ i( X3 v$ v, u, Z @
2 w( L# B7 i& u' v/ {' }4 l 1 Y" x, h5 }# I
+ t/ U% u) p% b. i1 _" _, U. M& c
fi.close() ( ], W: ^$ L. W: b6 n" [- ^7 X4 h- U ~ o X6 B
fo.close() 3 G- M. \ S* g ; {. S+ C2 V& {' X ' ]; o6 |4 r' Q0 w" z$ z1 F+ l( w
' w5 o" C7 R9 s2 e
try: 4 U9 i3 T; Q t9 n: C& L" Y/ T3 F2 `7 w& O* f( z6 _
# remove old backup file, if any : w( `) ~, O: }1 H 2 C0 z+ ^' h- J8 w; U. C+ O9 Kos.remove(back) % q y9 `8 z( N2 b: ?4 b) _9 w# ]6 H% ^ e6 T" W
except os.error: " ^, M* c$ ^# q% M' A( f0 S% Q ' O6 U2 z2 E/ ~5 v, f" |* Bpass ) e, M! B3 e1 j* H ; [3 M) F' N0 x- t # `. c! o2 h C) P5 S
6 E. x( E% G. I( B6 l5 R
# rename original to backup...5 n# `, P0 F7 R, K0 i) M
/ I% q& j4 E2 R0 O' S# nos.rename(file, back)4 e) a3 A9 o" x3 h, a3 g3 u& G
4 U' |4 [! }6 m4 ^9 R b
; c- |: ?- d% s+ N& Z
% t% W5 `" D$ n `0 F# m# ...and temporary to original 5 D2 u8 Z9 d; P/ G) I( y ! k% [9 W6 k+ r/ G; o7 Dos.rename(temp, file) }" j( ^, n0 U6 S. m
& V& E6 {* l3 S6 u4 B" D) y! U. w
5 A- y# a7 ?( G' Q* E
) L) a Q) Q# W, H0 h; `# W, m) y1 B
# try it out! " c7 ~% I% [ q n' r/ v7 t6 w% s) U - V+ n* L7 e+ `4 G( P3 m , j8 o; T/ y: S0 Yfile = "c:\samples\sample.txt". G/ p+ D& T7 R$ {
+ v- U: p, N, ]
( u+ s; Y- x; Q, _- G
0 p- F; J+ O; c1 rreplace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena w' o5 {6 K- i$ { ) r8 e2 g s6 T9 Jreplace(file, "tjena", "hello"), e4 m3 J" h+ a9 L0 ^
+ F! {" r8 L! S! K1 u
2 g2 u/ }( }3 Q( E1 k. h0 y9 J4 _ 7 f' P. {; r! ?6 q+ x x2. 目录操作) m: j _( U e& o
8 q, P5 P0 R. v" d2 [+ eremark: 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 L: M, o. [# L7 M
, [2 u8 {4 H# X& k9 r; J8 o8 eos.fstat(path/file)! k3 {9 n( z. b5 Y, d
4 o7 \- \" r% ]0 P" Y
Return status for file descriptor fd, like stat().