; P- s8 |2 c4 Z
" T( S- I# r0 H0 p P
os 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作6 a" U+ P3 L6 c1 {* ]' S5 k" S; r+ z
; E, l ~$ i9 F ]% d % c* n* x" ? p' o, `9 ~: E
/ X2 G- Y$ }7 [# g4 y3 y- `
1.文件操作 1 K7 d! Z1 a, O' P1 X. g/ z2 y. L* C
build-in函数 open 实现文件创建, 打开, 修改文件的操作 y. v5 @: H- ^. M 2 p+ s3 T2 `& L# C+ k) ~ ) I) G! o" T" e+ q; F0 W3 B ; U. M$ a2 h0 J* D/ b' uimport os* p; q# \% S* o" v' ]
& A! z R2 u7 A$ B
import string ( t+ h% W: ~# S/ X0 _ Z # J0 h, f1 t7 m r; k3 E# l" a: n8 D ' A i4 Y9 r2 |" I+ d: y$ c0 s# ?) A) c5 a+ L
def replace(file, search_for, replace_with):( l/ Y7 w u7 D3 v, G r& W
' B$ O- C2 Z; q/ p
# replace strings in a text file w a2 D; D; @6 ~4 @( x
, {) y4 q7 k, E0 @7 v
$ ~" Z4 z2 ~; w
( N0 O- i+ M& o( d V. G4 ~ g
back = os.path.splitext(file)[0] + ".bak" 3 [6 ~ O, d6 o8 j; Y+ j4 ^* h5 d6 T2 x4 h4 d$ z( p
temp = os.path.splitext(file)[0] + ".tmp" - E+ ?3 P9 A, n8 i# [0 M, n . `8 }7 ?6 |# L" ]) K / a/ t( e* O; G 1 d/ M* W, Z% k# [$ Stry: # x* N# o% E% {& P+ |6 ~: ?0 \' H. R' r9 x, O
# remove old temp file, if any; K" Z' Z) w7 B, c* ~, H2 j
% v, A: a: O1 l, }6 \( T' ros.remove(temp)& k7 j- S% f- p3 Y3 b. h& n/ r
1 M ]( r" p9 E; w Cexcept os.error:$ [8 H' Z7 {/ i3 m# M$ ~" }7 `
7 q1 Y) j8 j% ^& L( @% N# o
pass, n7 N4 ?( t+ [/ f' V5 D
5 S% [7 E, S& R3 s, P 5 a: h8 I5 Y' k# s$ t. M2 j. x( R, l9 O8 r% e2 t( N. h
fi = open(file) # 1 H5 I3 w8 Y- k) }2 I J. F3 ~2 F5 b& q$ y5 Z
fo = open(temp, "w") # 7 M7 l% C4 x Z' k. n$ J/ `" g, u/ _" e8 Q: A. m! x' ]) W. g
: j1 `, M; |& i% Z" E
4 @/ f! b& s7 Q4 D. Qfor s in fi.readlines():; P: H9 X2 {$ _: \# o
) L: r' s3 G- D2 i9 p
fo.write(string.replace(s, search_for, replace_with))3 I( P# s! e; h* Y5 f1 f
0 @# y( l1 R1 B6 C$ c- O 0 X& U2 R! |7 a2 g5 a& N/ @' C0 x) Y: C$ R% r* ?' [, P
fi.close() , V- m8 @* K" d; S b& u. ~$ u& a" F5 X1 W, ?) K1 `, U0 r% ^
fo.close() 6 f S# s$ o" P0 l6 a( z : }- _" e" [2 Q & g K9 {6 x+ W, z7 N. S ! q1 U! d$ W" k- ~try:4 N3 F8 ]' j! Q* f. @, B
& O' F+ w0 }! g* {/ y
# remove old backup file, if any & v% M* v* I' a/ T. K% O$ R 0 }3 o% D$ ^8 ]: J4 ]1 Jos.remove(back)( i3 I' [4 _) b+ h
% V! G% o: f- @7 Q
except os.error: ( @8 B2 X; i) ~* H8 L( L* G $ E! D5 M) g% x7 k/ @pass 9 \# r# y; R! J( S1 L' a( X; z $ A3 I" i% v# G. O( | 4 e) D; q3 \- N Z5 E
5 \6 a1 m& \ B5 P3 A1 V% x
# rename original to backup...4 L& L9 R1 O8 z
: m% J$ _4 v) O! f7 f4 f4 G
os.rename(file, back) % e! M$ T T( i: c, r5 G' J8 s8 F9 N& Y
( G ^( `4 q' D( }& C5 _5 Z8 m; Q
( o7 f [8 n, B2 l7 q2 X
# ...and temporary to original$ j, A+ X) v$ u1 Z& M0 k$ E. u# s' _
2 R% c" u" z: T1 C7 X& `3 Bos.rename(temp, file) 7 S; W$ A3 V; J% ]6 ~# }! ^ + E5 y2 X' [& e2 _9 w2 N / ?% o- z8 l9 u6 N: B0 g - U) u& b' ]3 V* T+ k7 F# try it out! ' ]3 ?0 x; Y) N A# b3 o( Y * H6 U+ @ ^ F0 q' Q* m$ A % U0 z9 N% {5 Q n ~8 R4 L. T8 ^" h
file = "c:\samples\sample.txt" / J5 p% e6 d0 D) j- J ! H0 Q7 A6 X% Y' u8 } j' @' | 9 `! H2 U( n J& c! v0 y1 p: H/ Q1 Y, G6 H& N' h+ {
replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena$ A+ S8 E" x) j! U' ]+ H7 f& \$ |
0 O: O: u/ i$ O; u5 P, }, A0 rreplace(file, "tjena", "hello") 9 y3 ^2 M M: Y0 i7 P 8 V# L0 C3 r t/ x' v : x7 Y" r# t) }% {7 f. I3 {* Q6 x! I: [: L
2. 目录操作 + ?8 [! T& S5 J& Y6 Q+ R/ p 9 D! q) v. s6 k$ D4 y4 Y9 _4 M% zos 模块包含了许多对目录操作的函数 5 G; B. ]3 s+ W; u6 ~& r" ` ( r& n6 s- a4 Rlistdir 函数返回给定目录下的所有文件(包括目录) ) K/ Y0 W8 w* G6 l0 |4 a; ^( v3 Z, a/ t7 b& l% \
9 }7 W+ i Y) u0 w+ v/ n
, x! {1 q7 b7 m) w" M4 a- {, k1 D& E
import os ) I. X; W/ E* p' D; h) {- \, Y7 g! v/ Y5 w/ U- p" V7 \
for file in os.listdir("c:\qtest"): 2 B' f5 ]/ }# b! j- b$ C+ D. _( e( a) Y$ o* S
print file 0 q ]$ ]+ l# Z9 d4 O( A. V, z1 V & g& u: M/ c' ? r% ^ 7 B* A) m5 M' H, s ( f4 c. Q* W# L* X h9 ]getdir 获取当前目录 ( s2 [8 ]4 L; A6 v+ n. e7 L/ K) d - Z9 x4 o; G" \. t5 _chdir 改变当前路径5 j! ?! i$ v9 `( P9 ]0 n5 V3 \
6 d- U+ L4 Q. B- B* p: A4 Q9 u
/ l! L3 f$ ^7 c# @
6 a0 W2 E- h6 Rcwd = os.getcwd() + K P! o2 q' r& H# ^) P5 o; H9 G& P# r2 O! u
print "1", cwd! t' j6 j$ @, [0 [- T' V# p# I
l+ r0 I; h+ T Y- `0 C
# go down/ K7 o+ |9 M: T2 R
$ q8 W- o; H: `5 ]0 g1 R/ cos.chdir("c:\qtest") ! I# g/ K3 o4 ~ ' e/ {2 l' a% B4 ?& @- s0 G. Bprint "2", os.getcwd() & S. b3 `7 }* X+ t. Z i0 B G3 x; o. Q
# go back up7 J( l3 d, s3 J4 S* r, h
+ @" [7 K* W. f. W; }! ?os.chdir(os.pardir)#返回当前目录的父目录 ) }6 n \1 f/ r " s8 L- N+ a/ o+ W! A! W0 m9 W* qprint "3", os.getcwd()& @! S+ U( S; m+ C0 \" l
. u3 G% K( h6 }
5 D3 ]+ b* [2 P# p$ h2 z; v1 ~; O+ ?7 K2 z; e* d2 Q
makedirs removedirs 生成和删除目录8 g, K- g4 k. S9 m6 C% B6 M
3 H3 s0 f% L. f/ Q% x, `
makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除 ) A" f1 J. G' N7 I" \ 1 \/ E- l$ A2 G! Y+ T ! T0 G8 e% K4 k; k) v
6 u" f% A8 `; ~ `/ `' Limport os H7 j! I" d9 F4 u$ @/ o . [& ]( k2 B# aos.makedirs("c:\\test\\multiple\\levels")7 b1 _& w( M/ [ |# u
- c# F4 {! L0 p& r
fp = open("c:\\test\\multiple\\levels\\file.txt", "w")& U8 [( o9 k. j' {$ V5 ~
. F: J; O1 x9 F4 f
fp.write("inspector praline"): H- Y% |$ k y; p
& @" x/ ]: a7 Q9 B9 V2 F7 ?
fp.close() ; p" C5 Y$ j/ _5 m% o# P) o9 M" {2 o" G* S5 X
# remove the file9 {$ j+ y9 K ?5 @7 U' U# w& P& P
- _3 ]5 y+ k+ t
os.remove("c:\\test\\multiple\\levels\\file.txt") ; D9 H0 F3 h8 L3 E( |' M. _4 |# e ; Q( p$ b3 Y( S# and all empty directories above it# ^" V- c* @# d2 o' A, n9 K
; M. t+ j, k8 @2 \9 y, m" d( w# P % K) ?7 V: @( P" R7 `. Z3 J
b2 ^' `, h/ Mremark: 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): $ ~2 ~- n: f' _8 r7 F# Z + \' K) o' |; c" ios.fstat(path/file)4 q" s+ T' B. T1 h5 E, w( {+ [
- a9 p; D; M/ `. MReturn status for file descriptor fd, like stat().