/ F. J1 C u; q. I4 v7 Z$ ?2 v l% m3 ], X3 v* x2 x9 t e
replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena( U" U% M- ~' x( _8 G
8 u7 @( \$ c1 J5 {6 w
replace(file, "tjena", "hello")& y- s) J. i$ P/ L6 ]' z
5 ]' S$ t3 ~/ z
+ o; h" ^; x- P% z 6 E* H" d, i0 |5 b1 U, w8 r2. 目录操作% ] z- ^/ _2 M. O: w* ~
+ K0 c( T0 o& v& T- p* R6 Y! Xos 模块包含了许多对目录操作的函数2 e# @; l' w+ F3 _/ `4 v* ~+ z r
$ b7 Q+ V# K1 `1 g0 l( O, G9 {* u8 M
listdir 函数返回给定目录下的所有文件(包括目录) + g- I: R/ j) `# W; I: J5 r" z+ F0 t0 ~# Y) @! o& k
% g4 j4 U& T# l: \* ?! z . u0 I/ a) \- a; D) Q/ N/ zimport os 6 R% T8 u% s8 U. H 7 z; e |, c4 @for file in os.listdir("c:\qtest"): " I& W( C2 D* A0 e5 h/ v3 W9 _* A 7 D7 J+ Y8 ]# @! zprint file* R6 |* {9 I" ] A" _+ h
5 L3 M( C, T5 x) a, ?, L5 B
/ t* N, t/ l5 F
4 \( d8 ^* h5 y7 n+ z9 Wgetdir 获取当前目录 4 L$ Q3 G0 R6 I, ]8 F6 N7 i7 W 8 H. r8 k3 }- e' L0 \, @6 Tchdir 改变当前路径 6 T( z, I1 U1 s D3 ^ M , v- x: c1 X' A7 b! L) d) e* [ - R6 U* w3 h3 {, s1 x/ s
: @6 {7 s# ]# i
cwd = os.getcwd() 2 @9 J8 f, j$ d; L: l0 g5 O5 l5 ?8 ^8 I" G
print "1", cwd" n5 w+ O) q; J g
. Q" k. a: t0 q5 A- l! `# go down ' [3 D6 _% j% c2 @ d0 E9 V / v1 y2 h' {+ ]' w* Aos.chdir("c:\qtest") ( E1 e, U/ B3 _+ ]$ F' i0 c6 G: c' D6 ~7 n8 \- X: L, D% n
print "2", os.getcwd(), y# h7 L: p: h0 a3 ?
( H8 x8 G* Q; |0 o- N0 J
# go back up . s1 U/ Q K" V: x3 f& t& w& Y- d6 C }
os.chdir(os.pardir)#返回当前目录的父目录 . A6 w* ?! H. k2 p- @) x7 q/ {6 E7 j7 x: J0 g% q2 r Q1 v
print "3", os.getcwd()) o1 i. B7 F$ k; l
# [* ^( H+ J/ ^: c; z" R% X 4 b/ s7 l) W% k; r0 ?0 O ; h$ v1 h% I0 G4 mmakedirs removedirs 生成和删除目录9 ^3 v4 N- x) y% H. z
+ `# a$ r( k0 [* j0 H A. |print "- last modified:", time.ctime(mtime)- U" w& I. G( g! V1 b y
( }% f8 I6 W5 Z/ {" R
print "- mode:", oct(mode)+ r( Q8 ~ p' f' R
0 u% d, v+ [! L2 Iprint "- inode/dev:", ino, dev6 J( W5 B, W1 _3 t' m7 l
& ?" _9 t J* a$ J# r& ^7 w
3 L4 @2 }4 j. l
) ?2 E. S4 I+ k6 d2 Y& O
print dir(st)6 y+ \" j e0 N( W2 @# h4 H
! ?% `, D6 r% |8 ?* G" ?, \
print * K. ?! c4 K3 s; P
; l, ]% h, R# o8 X
dump(st) 7 F0 M$ O$ E) ?! n: s5 ?3 b2 j6 J4 M
# print * v4 V" d: J1 i& `+ |! X/ F+ v ( w3 o8 `" f0 @$ U& V% k. j8 F & K" K x. k5 @% X' I/ H, w& n
# s1 H" x5 ]* g) E0 i: Cfp = open(file)8 {5 B1 t& A1 j+ n' i. o8 B h0 g3 H0 x
1 m6 }0 h3 j O$ q; i3 \5 ~% Fst = os.fstat(fp.fileno())+ V7 y& r# @0 R
3 g' O' h* j* I g: g
print "fstat", file# y3 ^$ E* m" R' V* Q/ I2 @
# T ~" B1 [3 d$ d: f9 N9 ydump(st)5 b! C: l u7 R
1 e; V5 @' n' K+ f/ q
2 d$ a# v3 B. b$ o" {( E! \2 d- }
n* [; K7 K" y0 |
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): 7 Q# V0 [' ^( a6 B0 f7 e * Z+ k9 O# y& Q, _$ s$ kos.fstat(path/file)7 _/ |7 a8 z) i: K; K" l
, X% r- a9 H: ~, {Return status for file descriptor fd, like stat().