5 O8 q* T0 M4 _: R! h" v5 { 5 u. C3 `, F0 q+ i( J3 N
5 G- J0 W' }' g6 P$ ?/ X# ...and temporary to original ; J6 N! [- F( Z* Q 0 m) e" q0 X( r! r# X) Hos.rename(temp, file)/ U5 ^6 T. g: F+ Z
- m. p( m# Z( q) W& Z e5 s J , J6 r" U" F! o# K. G- `- p
7 V: @. D D+ w2 S
# try it out! 0 n. m) k5 v S4 U$ {7 ? & g2 p% P' @% ] - |2 X( b$ L5 S/ k; W
! V7 T. ?: G3 I0 ]4 b% m/ q
file = "c:\samples\sample.txt" ( S9 P7 ^6 H5 R% e [ A8 w0 u4 g" Y1 O+ }# r$ M7 \
% I J F4 B4 E3 S$ g4 N % K/ q! i& @1 j" m7 r: P+ j5 u3 kreplace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena ( z6 o& B7 y6 ?0 L. U % e5 | ~7 ~4 [% dreplace(file, "tjena", "hello"): m) a* I1 J# n0 }& i* N
% b. `3 n P; m8 @, g8 N
/ `1 {/ d2 ~! u, S
$ `7 i u2 `$ Q: A- ~3 ^2. 目录操作3 ]& e# r x5 h
% Z9 t7 g1 a# X( Pos 模块包含了许多对目录操作的函数+ a* l4 x0 ?: p M- H
! v7 ?0 H0 X# B
listdir 函数返回给定目录下的所有文件(包括目录)& A. Z, u' g4 B3 S
4 U3 r" f. F8 y2 T
( U6 ]9 `6 z; l E1 r+ a
* [# V o$ f+ g- ]( S1 M8 bimport os/ F3 U3 \1 V$ u, h* g3 A1 [
# L& L% y3 t8 T
for file in os.listdir("c:\qtest"):: I& P, c1 z. f" b
6 m9 J! a" D9 Y! l: {print file 4 s4 R) y/ K2 `! M1 j x & y( R* I0 \( J q8 t : r& d: A' q1 C& j8 e% v1 ?$ \& } f) k w5 x% w& t& R, [5 qgetdir 获取当前目录 9 u3 O5 B. I5 ~; C% L3 e! ^! T3 P0 e1 X
chdir 改变当前路径. x, N: A" H& V# ?6 `, F
( G9 O" N. |0 L$ s' B! U; O5 rmode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st * U5 _7 ~5 @ P& E4 ?: `. G ; m/ v8 ^$ ]; P: Z' r( j# N. Pprint "- size:", size, "bytes" + n7 o) c, r. `4 e 3 f5 o( e+ m$ n0 Q! I& Y3 A4 w. p, Lprint "- owner:", uid, gid' h6 A- D1 i( l. d7 e
& W8 l) n' G) I6 Q' [8 f
print "- created:", time.ctime(ctime)9 T: W9 _3 T; c, ^/ c
# F6 k. z- I. ?/ cprint "- last accessed:", time.ctime(atime)5 g2 {3 N# C% l
+ o( c# W2 g5 f3 H" Y- Rprint "- last modified:", time.ctime(mtime)0 [' M: i0 [: }- R6 o
; W3 p5 y# b6 j4 J3 W/ N* Z
print "- mode:", oct(mode) ; g5 o3 {* Z4 ~8 h, P; t0 J0 b# U0 s3 k9 J3 d8 D8 d* @( Y+ s
print "- inode/dev:", ino, dev$ n \# m8 b @, c% E' b3 E* d
: [% Z6 q z, S5 R4 y/ H
' ~7 W' T; y* w- |% K! i7 X$ X ( {! l4 N; S$ aprint dir(st) / w. K/ n7 b; U; K : u2 a4 W. o0 Q8 yprint * X3 `7 _1 T, j, p ( t- G, E, ^- cdump(st) 0 Q% L9 |3 t3 P& J& u5 F - t1 `5 {/ O1 k* S# print . a L6 W0 g/ s( q/ Q1 [# |2 Q" p* k6 o, L, o& `
+ N% ~5 T6 ?0 L% O" D* a + y$ u, e- s) gfp = open(file) ) E, a( s! k/ e! Q' R: v( I: F ' b0 s* h+ u t3 U! c5 ?st = os.fstat(fp.fileno()) 2 t9 f; {, s5 ~) s/ R* i* {3 ]1 A$ N( w2 A0 c
print "fstat", file % ]! P! {) {1 ]+ Y/ b$ k * c) k$ Q, ^/ i7 n% gdump(st), u1 `9 R" B* E1 [* T6 Q4 {
# Q7 J# g! _2 `2 c% C6 ~% K! }
0 j9 t! }0 N ?
) l( U4 J; L0 S6 \( I- Y; R: oremark: 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 G) E5 m9 Y! V) b" k1 F8 ~2 J7 Z7 N. S; F) S8 i
os.fstat(path/file) + E; D5 ?+ X7 {3 s: b) |1 i- {. H! f) Q# L0 S8 M
Return status for file descriptor fd, like stat().