数学建模社区-数学中国

标题: Python OS模块学习(一)剪切 [打印本页]

作者: Seawind2012    时间: 2012-7-10 13:49
标题: Python OS模块学习(一)剪切
. g+ B: d; A$ Q. X9 l' P
; _3 _$ K- f7 ~7 @2 I$ T
os 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作
( {: G& P8 o& m6 o# H! c( ^- a1 h  B) Q& |; _+ i/ Y5 E. x
   $ O% c, l* s/ e+ E+ f

0 ^9 N% I- N  N- n1 @1.文件操作( z8 Y& P+ [8 K* v
& I2 E+ O$ K1 \$ w4 K
build-in函数 open 实现文件创建, 打开, 修改文件的操作
" s5 i1 `  u' j7 v: C, M$ Y0 h
" \5 m) p, d4 c/ t1 e " _- a6 T( ]& f8 E
7 x6 y2 n, a' I5 d: Q8 V; e
import os4 \& L4 S& N8 [4 U
  _8 |% V3 [! T) m( B% x" j+ P3 ]
import string. j: L# }. @" K+ @2 S9 Y" Y: K
4 J) ]& A! n( ^
   
8 K: o8 U& g3 ]% |" p$ R; M  f1 i7 Q7 V$ v# i( W1 L$ }
def replace(file, search_for, replace_with):
( `9 G- J: P+ \6 z2 z5 K$ [7 P. {1 g$ D& i3 p
# replace strings in a text file9 A% L2 m) V, D$ \! }

2 r( [4 ]7 \7 ], A. n) o( ^   
3 r9 R5 n7 h0 ]! j7 V1 \1 N$ w, _& C% P' U3 z  I5 i
back = os.path.splitext(file)[0] + ".bak"0 L2 D, y1 E$ U$ W

7 \6 ^, q$ I# W1 y3 \5 p/ T+ F' \temp = os.path.splitext(file)[0] + ".tmp"; C$ b. ?1 ~- O& m& L4 B. Y

# x9 c6 ]" O' Q: k0 S   * |2 y& X) H4 G$ ^$ n: s

0 [: z$ [( X/ |9 y+ Ltry:$ j$ D2 f! s4 b; E$ o
0 Q( J  N4 X$ _- V% |; g' k
# remove old temp file, if any
% v! s% O5 d% m  h: d; q
* c; g6 n" G1 [os.remove(temp)
' d" |+ g  R- P# Z* p2 ~2 z, ^0 r6 w
except os.error:
/ ?/ O/ ]' p! h) S4 |' u) ^: v! `) |* m! T( I2 p) ?$ B& E8 H4 d) I
pass
% u3 H7 j5 y1 N+ v% Y$ H9 f$ \* @  Z+ s! h, d0 p
   0 W4 _# U/ S9 ?4 q7 s8 D* X& Y

2 L$ \6 k! [* q) E/ @fi = open(file) #
+ B/ @8 O, f: ?6 \( Q1 g7 h8 x7 g1 n/ O8 M
fo = open(temp, "w")        #
% B$ t4 F8 C" n; Y: L
" |" k  B$ l+ m8 }! D6 {   # i( [1 _5 F9 D$ A3 ^& A
" z: G3 @' t+ R2 v, l- O; `
for s in fi.readlines():
1 U" f( m1 R! N9 F- N1 f& ]1 d  S' _, D) ^( b/ W: l2 m1 e
fo.write(string.replace(s, search_for, replace_with))
5 n5 [9 O' Z2 ^: j+ O, U" K* N
0 t+ H2 f" {" i0 i/ }   
0 Q* }5 ^6 P8 l6 U8 z* F) q/ s. k$ Y& p( c$ _/ D
fi.close()
4 l- M8 \9 y1 r
" y& w9 I2 H3 D# Y- gfo.close()
8 O$ Y# d6 h8 X$ x$ D# G7 {0 G% D; v4 W; @9 d9 W5 O
   
1 j) r1 ^! \0 I# b: a
( Q) H8 x9 F" x% g& W+ [( O2 i/ A* G5 Jtry:7 k  h5 t* s9 a% q) {: e& {+ S2 w
' m& W- T% x) `  v
# remove old backup file, if any' c: X# D& C3 w8 |: O

% G! Y6 Z- T9 C. W$ E- L& ios.remove(back)8 J* d* x+ G4 k3 B0 e/ A( ?
$ E) f" x  ]2 E- K# ~* L
except os.error:, n5 m& M6 |8 Z' h& ^, ?

4 N* L! \  i1 e- I% R% F+ Fpass
6 [. y/ h9 N! C2 K  i% l! w6 H
' }) l( j7 U# ]0 H  j# D) |   
0 a) s! s: Z$ [) J4 K7 N
& J* X, b3 n8 J9 ?+ ]- s# rename original to backup...8 n3 S9 q& J& M3 K
( f& N1 l3 K) C
os.rename(file, back)8 ^+ v0 n- k9 x! [: F: Q, ?

% l  U/ T: |6 W   3 V$ e6 t$ E, y/ V
. n- C% ^* f# B. p, N  b! g' P6 y
# ...and temporary to original
- F6 r, ]$ Z" I' f% Y* }5 ^7 l: A7 R# s
os.rename(temp, file)
2 w2 Z" }6 ~) W
, T1 z0 p- E, A   
6 P( q4 ~( G! e6 B0 }1 m# P$ [! E# V' M2 S
# try it out!
/ Y; N4 F- Q* m# b% s
* s8 N% g# A3 G; }   / M3 i0 |8 r) h$ D$ Z
! r2 h+ G, g  r  B7 O7 R' _  r# r
file = "c:\samples\sample.txt"
; u+ F# f+ a$ p- P, l! n/ N  {) q7 G( |& s# V* E- O
   4 P$ y  N# D; m# `" B2 t
& U8 U# F  T2 E
replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena
" t  E0 m2 e+ o) F8 ]$ E. u+ V
+ u' [3 ?- R( `; A& K/ k4 O4 Creplace(file, "tjena", "hello")
  q. j7 @: A  y& v: j
# z# k) G& ^; N' r( ]   + q) s  o/ T8 W" B8 r7 d

' V+ G/ ~, M+ z3 g# T2 w2. 目录操作% L% t! s9 P6 C9 `
( V; ^$ T* |! p1 D. X
os 模块包含了许多对目录操作的函数4 K3 @% L6 ^8 c  t

7 w1 v2 c6 y* n# ilistdir 函数返回给定目录下的所有文件(包括目录)' a, G( e) _  _1 p3 J, {- @0 A, I
$ L2 N  E; C  U: x+ S
   
; v. ]# l3 T2 L1 G5 N6 B
; @$ C( N$ |: _. L2 @4 Limport os4 v9 }* f* S: O6 _5 G2 s7 g% x

. c$ T" g$ v/ `for file in os.listdir("c:\qtest"):) ]0 M6 {' g/ N/ {) }) i! c3 M
- |8 U" p& E8 H- c
print file
& f' x+ A2 v4 f2 q! D
! N2 `( _6 o/ O; v* a( R" t" b   
2 n- q, \3 |' @6 g. u, R
" q* r3 f: @4 i6 ^, Kgetdir 获取当前目录, |$ _& X7 L/ c% I+ ]( N2 t

, i" V. h" w( G0 `4 u& V4 lchdir 改变当前路径
7 ^0 }" D. X  P# Z/ _7 Z$ H" T9 w" M( J. M- r4 v8 b4 e7 _" Z
   
- N3 F6 t! C9 K3 J; t0 F9 k
* f, D* X0 Q. E, V4 w$ Dcwd = os.getcwd()/ B: N4 I( e; J# }) t

1 \/ \7 h6 o- K- m- Iprint "1", cwd+ z% ^7 s+ A( w0 L2 _" X6 s6 k4 U
' ^7 ?5 |6 ]5 |: t
# go down8 B8 r5 \6 c# d; ^9 X: B# J
  c- J6 x' s( J3 Z. [
os.chdir("c:\qtest")
) v; {& Y0 W, f' S, j) N. U# `% k7 k: \2 ^9 v0 L3 b
print "2", os.getcwd()4 n% X3 u0 }1 n9 q8 E% c* Z
) u3 U; z5 Y5 z
# go back up* R0 ^& g: @, a1 v3 n
+ ?% ]8 a& i1 @
os.chdir(os.pardir)#返回当前目录的父目录) F# e2 m7 S+ I. `% S

* q4 U7 _$ s) p  Z7 C5 ?- fprint "3", os.getcwd()
' x, E/ G# \5 L4 H$ q
( O3 E% Y4 g! h6 X, b& S2 Y) V# [   5 H& R8 @0 f. S3 h6 F" }. q
# V- {5 l& b1 B2 M' W
makedirs removedirs 生成和删除目录  [6 P# Q4 p3 e
& X; k5 [- v8 z1 U& |
makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除) Z" Y. Y' L  W) O. Q& P
; c1 s5 P$ p* ^1 v; T9 X1 w
   
0 E1 E; L7 l$ s2 l" e7 v( s4 Z' ]/ v: T
import os$ @: e3 O( x* O& n- f

1 u0 s! I0 y) @) j$ t, v% q% Y" Yos.makedirs("c:\\test\\multiple\\levels")8 g$ B* ]8 U% K/ V
; n9 c" m5 ^% M( {- v. s
fp = open("c:\\test\\multiple\\levels\\file.txt", "w")
, K1 g' W/ c7 j; M8 r8 H5 M! M* u; d, x# A
fp.write("inspector praline")& ~2 Y( R8 J: c# a+ T9 o& l2 u/ y

( Z& h! o; v% Q6 B4 H3 u/ gfp.close()- Q8 H* u) i; O; R5 B6 T2 m
9 k; I1 ]  H+ q4 x* C. m# [" N
# remove the file
4 j( ^3 L" b! s8 d1 n  c% K6 I! t: c8 M0 Y/ {" R( s
os.remove("c:\\test\\multiple\\levels\\file.txt")
$ Z4 e! \' J, X. Q1 y
5 d0 _# F7 W4 Q# and all empty directories above it1 [) o  f( \# D8 Y

3 o$ t5 r( W: w$ r+ Cos.removedirs("c:\\test\\multiple\\levels")
' S2 h# a# V9 }5 h% y: M
5 ^. M3 e0 A/ M# F   
4 y6 E! D6 N# i. g! j( I3 U: t# J0 v+ {1 f
mkdir 和 rmdir只能处理单级目录操作.
+ f* E" k* U- o) O! _) O# u6 U0 [6 X5 R* j; u. b
若要删除非空目录, 可使用 shutil模块中的rmtree函数0 s1 G$ x' r: f- ]! V( ~. B# j

  C4 i9 G# Y+ Z# S: D& u   8 ^8 P: N* x, P& a9 m6 G

7 ?7 j2 {4 _0 |6 K6 ]: D3. 文件属性的操作
5 [9 z4 |; r; e2 G& |6 ?) Z2 L. V6 r& t7 p% t
import os
' B3 V# s& o( E& }3 _* T& Y( H' G, g7 z
import time
5 j& E6 S  P% c% F
4 ]9 Y3 Y' z5 n, k" ifile = 'c:\qtest\editor.pyc'
, ^" d" Z- J  l! ]
* B. K( n+ J4 R, N# b   
7 B* a+ s& p4 I% t
$ S& l7 t( W& T2 ]5 _6 {  Y: }st = os.stat(file)9 o* c+ k: m9 m  F! \0 q2 C
: C" d: Q* E/ b' e
print "state", file+ u5 _4 Z/ f* m7 s0 M- O' e

2 r" r  r& T9 q0 T4 T/ N# Y" f  K   / p3 K+ f5 J# G  j: J" C' q

9 R5 A2 J& I7 P% E  Xdef dump(st):
/ D( C& T' y2 {  Q, c
+ u& O; \0 ?* R* l9 c8 z# w0 pmode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
) q6 y3 ?: y1 U7 I' k9 A( x' ?9 c* N  |
print "- size:", size, "bytes"
& x8 Q1 a, n" F- C% H3 U
: G. }* r% O' a2 \. Tprint "- owner:", uid, gid
3 ~. v# _2 H  \) i0 ~
; o* a7 x9 Q: p; a1 w$ Hprint "- created:", time.ctime(ctime)
9 c  s9 c+ m" R9 K' `$ L
0 o, Q2 I$ }# O1 P3 {4 x, Nprint "- last accessed:", time.ctime(atime)
2 w. N. e$ D* R1 _/ Y4 F( O& _; f5 F) R8 U& u$ E
print "- last modified:", time.ctime(mtime)
0 ~1 }: f3 r) M" o% m" Z! k7 r, T' l: K8 ?& X
print "- mode:", oct(mode)4 X' V( k  Y/ R1 P/ v, B

( D3 u8 P9 H) Hprint "- inode/dev:", ino, dev
( t4 Y' p7 T- H! T" v( N7 e' ?  U1 r  h- c
   # M+ ^5 @0 F+ r0 ~

& @4 m. w8 X7 Q* [4 Zprint dir(st)
3 X6 Y$ Q# b: ]! k' v, Q# Q
  ?6 n8 \, n2 e! xprint        
: [' R9 h. _; Q% O0 x
/ y, O; H$ e* U& L$ H' c: T+ }4 X7 U# ~dump(st)
2 z+ w# ?; j  J- ^1 |+ t
  e- T3 }6 v6 v# print
9 _8 U7 Z# [4 i
7 k9 F% u7 o) q$ o( j   
% ]  z8 B2 g7 l9 m3 q5 S, I, W" S. ^4 o5 l
fp = open(file)
6 l: |7 L8 P- X+ K  g! G% m
# {4 j; _  S$ U6 Jst = os.fstat(fp.fileno())
! P4 T. ?3 a6 q2 |9 W. D
2 y) J% d- j1 i8 f. Q/ O  f1 Wprint "fstat", file
1 g: m8 k5 h, Y% k" M  `# q- W0 b9 }5 g; L  r4 G9 p
dump(st)4 |- j) q$ w2 U0 K  \+ W5 @
- S/ x" @6 b) `$ D: |% J
   
& w+ e$ j, S& s- m% d
! R3 n6 d9 ?- Q7 t9 r' G$ vremark: 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):
# M4 g8 @( e4 @
1 X; F- ^# x9 g4 L; _" ~. vos.fstat(path/file)+ A7 W+ _. h/ |# c4 N- }9 y  @/ \* W

8 j' f; s5 o- f4 A- \: _# `Return status for file descriptor fd, like stat().
作者: darker50    时间: 2012-7-10 16:38
  可以放进文档当成附件下载的!
作者: Seawind2012    时间: 2012-7-10 19:00
darker50 发表于 2012-7-10 16:38 / r9 C2 S2 k4 [' t
可以放进文档当成附件下载的!

3 \( g; O0 H0 n; H  j. v很好的建议哈




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5