数学建模社区-数学中国

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

作者: Seawind2012    时间: 2012-7-10 13:49
标题: Python OS模块学习(一)剪切

7 D  K% m# b& ^; i; A+ l  u7 f
; w( W7 _2 N$ H4 s) jos 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作
8 D: O/ ]4 J$ G! @6 V' U3 `6 P/ H
! @6 g- z6 W) v& x) L8 o5 g; _3 t   4 [3 ~8 ]7 s% {) q' L; ^

/ \' o7 m, Q/ S1 i5 |1 ^" ~% [1.文件操作( E; R3 S. N; B5 Q  H. Y  X

4 q% D9 k3 P6 ^( ubuild-in函数 open 实现文件创建, 打开, 修改文件的操作$ S0 {3 B0 b$ g4 g

6 V! a# t1 d# p0 E1 V 2 w/ p; m. c/ N6 I, v
4 u5 X, A9 `7 `; i) }1 E9 W4 L
import os, u- _" k/ L. B4 v7 T8 K4 O% J

. a- z, M0 r* P% P' A" h0 Z: d4 P  fimport string
! h3 k' r( u) o- H. `+ u6 Q) c! l+ [& A* y/ R4 T: S& O) g' u
   3 S0 I/ n5 A- x" j

1 B/ l9 }4 Q! @% _) J) ~7 U9 B- ~def replace(file, search_for, replace_with):: M" }$ v" `& N$ b
  Y& ?& m: B& k  ]- l# K5 b. [9 F! i
# replace strings in a text file5 E2 w3 y/ ?) L/ ?7 ^$ B

' z# ~; g: g" D% o' [! ^5 F   
% s3 a2 R6 v: a  Z, n2 W
5 [% y, w% W3 P$ e0 j% x/ q6 ^( Tback = os.path.splitext(file)[0] + ".bak"" ^6 R$ t) Z3 C8 t1 Q
4 l0 u$ R, s( q$ K6 m9 W" l; A( w
temp = os.path.splitext(file)[0] + ".tmp"/ V( z5 M8 R0 U# [3 q( o

7 Y- z/ n/ f, y  t" d8 I( g   
2 |8 w1 J* D3 [: T7 {$ m. G7 P" n' f
: t# W0 a! c  a% P6 H) m( G9 otry:* s" J7 o$ w. c) Z3 t9 L

9 _* E3 w0 f- J! X; P" t# remove old temp file, if any3 x. ?* e% r0 J/ p; h$ o

4 p6 z2 i; C8 e6 o6 I$ _: ]os.remove(temp)
% P" d  w5 l2 P6 s$ c. K; ?' g, r9 w8 e+ \; o
except os.error:& b6 i. U2 V; N% L

: z$ }* |# [9 M- i8 Z8 r0 K! Dpass' O/ a  Q+ B2 ?

; ^2 X* D0 u' D2 o   
, D; B$ {2 I$ V$ }( r% u% d! ^  h( h/ k) ]1 w; h* k
fi = open(file) #: A2 l" Y5 i$ D  }
- Z: A, J2 `+ U; \! L3 E3 d" u
fo = open(temp, "w")        #
8 W1 R% e: B! }1 I& B$ U: s9 p* j2 I2 e" w! U
   
9 C2 F: J! X( B/ u+ P
& M% S; w, q8 Gfor s in fi.readlines():4 ~) k" H" U! q. |  I+ x$ q

3 M& R# b9 z' s1 j. Qfo.write(string.replace(s, search_for, replace_with))
  O' P# }3 }5 }; h" O$ v" o9 l; }  d5 [9 o8 R- K3 A6 ~% O
   # F/ Z! g$ q- \3 Y( C8 O: U0 c; Y

1 [# e+ @- n9 O. ]6 E% U7 efi.close()
3 b( `0 \  m& L+ \* A8 D9 c: c
fo.close()
; A6 j# A+ E' h, Q
9 s* U4 z0 W  j/ @' ?* |   . C8 k5 u2 W2 s6 T$ g7 q

. K1 p, S8 Z+ @' g7 s" V; m$ Jtry:
# J# E2 I2 f; g# i
2 k. R6 _1 {5 i; h0 Y2 N# remove old backup file, if any
4 `7 _7 u( t4 @6 e2 Z1 u% U* L" z4 I% K3 r9 d4 h  X
os.remove(back)
  W% p0 @( t3 Q: B- z+ {0 W. w
; ^: w) k1 A9 S% W. c3 cexcept os.error:( v$ B! p" j! ]# z5 V
/ g; Y& }5 ?  w( }
pass
7 }6 U9 T* G  |. g/ t! S0 L1 z  A- q1 N2 C
   
4 J2 M$ q3 o1 a$ @- z" ~% \' `+ \3 |& n1 v! Z& e
# rename original to backup...8 p' Q/ }/ E1 z  j5 \" h$ a$ G: `
) k) P% e% {) b, ?7 g
os.rename(file, back)5 D3 y$ \. I& F& X& L/ b
0 _& t* X: X& y6 K) f# l
   
# J! T2 k. o4 _6 h3 t& I9 d( [% v/ C! L/ K3 _( q- x7 }4 L
# ...and temporary to original$ K& N" K8 o3 ^& M$ Q: _
2 J/ L9 F& s# _
os.rename(temp, file)8 I. j/ k9 M. b1 w% e3 j, L( P

. s4 a4 n0 M* r* Y  N   
" g0 U) m) s( k7 @4 F9 K( a; U: D( H3 H
# try it out!
" d# G  e4 E) {& k- d3 ]( r
' {2 Y6 x  ~( J3 _0 B/ D   
6 W- [+ V9 ]$ _! O- V, Q7 ~5 v  L" _0 T# s2 K1 ?4 F1 U
file = "c:\samples\sample.txt"
5 F" r1 |7 i, g
+ [# G7 t" U3 c   8 r5 j9 x& E6 ?5 o/ Z( _( ?- A2 A

4 R0 j6 v( w2 n5 J! V  p1 |3 }replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena  p  S. x+ K; H- K1 K

/ L5 N+ y; |! j4 Qreplace(file, "tjena", "hello")
/ i) G4 i7 _9 }. L  l5 E! w1 ?/ n" F* `6 s  t! T- Q: S
   
1 m( ~4 Z* o- R% k4 e$ O* ^
0 J6 {6 I0 o" j9 W& j& D, K# J2. 目录操作- t7 S9 L) A! x( f# w2 q

/ ^* R  U9 M$ H" Sos 模块包含了许多对目录操作的函数
. b: C4 h* ]5 \& i, F
% f6 c" q* ~: blistdir 函数返回给定目录下的所有文件(包括目录)
( W/ K+ z# O4 V9 C$ i
, j0 l3 n% h& v+ o   
) z8 t& b  u% d( f; {' E7 h- Y
1 t) n' f& w. ^$ [( t3 Cimport os# ^* e& W: q. J( v6 g: e, r; S

* l; e; u8 g6 Lfor file in os.listdir("c:\qtest"):% A9 D; [: m, b2 ]& g" D4 n
( u4 z; ?# H! f6 Q1 s. f
print file( e3 Y& E, N& z6 |

" |7 ~7 {" |; D7 |   
4 f5 B/ H( h( r+ M
- ]1 L. V, N( M9 Ugetdir 获取当前目录. R. n$ A5 Q  o1 T. B* a3 b
% c. ~; A5 v! W- }, B! W
chdir 改变当前路径( n* m! Y, ^4 e+ [
! H! `* v0 s4 A
   
4 B+ [5 B$ K0 a$ t% z4 }( r  ]4 t4 q3 N; K
cwd = os.getcwd()
2 L9 E' Q/ M6 x# r9 Q. ~
  i( y7 N. S% J5 y1 Tprint "1", cwd
( d4 Y; p' U3 \! M" M- S4 |
/ j9 R4 `1 W0 b$ V, f# go down; x: _' }# z$ ^' C2 P8 T

! y# I% L. e. {; W) E* bos.chdir("c:\qtest")
% S) n' D7 \' X! j2 G2 B8 A$ c0 C6 H# Y- [
print "2", os.getcwd()
# q3 j+ I( n% r. F& D3 U( t0 z2 a( P: v" ]- M7 D: S2 }
# go back up$ b; P9 N& Y$ W: @

$ J; E4 ~/ b( ]os.chdir(os.pardir)#返回当前目录的父目录# S" ^) m' B: S- {- a# G" {7 ], z
& \7 U; d; N% V% W: E2 N
print "3", os.getcwd()
, s; ~( c# c. X. d" T5 n% a2 u& I; S5 A
   
& z8 @7 R' u5 d; ^5 n# Z
2 j+ K% d+ z1 x+ d  b) T% v& Y4 I$ C, Qmakedirs removedirs 生成和删除目录6 l; A6 b* ?3 [* n7 i

3 y# V) k6 Z, N4 umakedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除3 n8 G8 o6 v6 D2 g0 w
/ p9 p4 ~5 }+ [7 j
   1 Q8 s) n$ v6 J- u8 u: x

5 V5 ~; C1 |% Yimport os
+ |% B. ?! u8 e" p1 e
8 k$ Q% C6 S, \: `3 Q2 Aos.makedirs("c:\\test\\multiple\\levels")
- m& B3 |1 R; r7 |
/ B4 F3 B3 P* P) ^( {! I, hfp = open("c:\\test\\multiple\\levels\\file.txt", "w")9 J' J. A7 S+ i* H. S) w
+ I' u# \4 g" }; O* s. f
fp.write("inspector praline")
0 c# \' H5 f9 q9 n7 W" i) x" Q. b) e  y  x" c; E3 R
fp.close(), J8 F6 M# `- B' \# u

% o2 a0 [1 g7 [1 ?2 w5 |# remove the file- |8 E0 c, ~9 V" r- K8 d- i# _
3 k# p$ H/ e' X* s+ J, x
os.remove("c:\\test\\multiple\\levels\\file.txt")- C! N3 \3 ]- k9 E7 `3 _
& d0 o4 S- X& z5 J
# and all empty directories above it! n& Z9 M4 Y/ {/ z

. L( \+ [/ e1 p' b3 d2 g* ~os.removedirs("c:\\test\\multiple\\levels")
( o$ X' R# ?# B7 p. V3 W
- G: z+ y, `/ ^; b  B" g; X   
( k' E3 J. y5 k; x* q! l8 c- n* Y+ y4 P
mkdir 和 rmdir只能处理单级目录操作.' Q: n$ Z- l, c0 \: J% ~

( s& B, n& t6 E+ T3 _% U) _若要删除非空目录, 可使用 shutil模块中的rmtree函数
, L  V  t8 }/ M9 O0 _$ Z$ z, V; d; X6 G8 M
   
* c" c) d0 F2 \  Q. y, G
1 |/ ^# Q9 n+ C3. 文件属性的操作* y5 k9 O, j* C- P- T

( A' M! g& m! c6 i9 t- t  Aimport os
- t; _8 M* X6 l+ ]
; z1 i: j5 u$ q& b1 s1 ?9 {* }/ Bimport time
: s) p( q9 [5 U6 Y" L  {9 e! F
/ y9 r9 ~5 n0 Y4 @- v# F" Ofile = 'c:\qtest\editor.pyc': ~0 p( X9 a( t9 o* ?

+ B2 Z* {" Y1 F7 z7 g   
0 j$ B- F, F: i$ ]* t
: ^2 B' K0 }( S* Jst = os.stat(file)! L* N: ]! Q; _! J6 l6 s0 x" ?

. m$ i. n" a  G1 \- Pprint "state", file& l( T0 i& b2 r& ]4 n: [2 Y

8 ]& T6 j% Y/ Y8 ]/ n% k   
3 H+ c7 p8 t) f4 g" `: z. h. c% B& X0 B% U
def dump(st):
5 t" ?7 X$ d4 }! x3 T6 g& c3 @8 U
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
# C2 [: S7 }2 E9 _9 Q6 h* d1 K. K9 _# p, D
print "- size:", size, "bytes"
. ^0 V2 n% {% n6 L: w1 V/ O- P( [+ _& g# O& @1 X! Q+ _6 H: r! r5 w
print "- owner:", uid, gid
8 ~3 h& F6 M0 ^) C$ R" d; L1 n6 f. O0 j! Z
print "- created:", time.ctime(ctime)% j1 m; t0 K3 j

+ A+ j/ A: k/ ]1 {! jprint "- last accessed:", time.ctime(atime)
% P$ L$ R+ K) h8 `! G( H+ a# v
1 h; F* I, p9 h; Dprint "- last modified:", time.ctime(mtime), `1 L2 n* {3 l- F9 s

: L% h3 I  s) V: X( I! K9 G* \. Tprint "- mode:", oct(mode)
9 H$ S2 c# I3 i4 a  @
( G$ h( o7 y1 Q9 E9 h0 R6 kprint "- inode/dev:", ino, dev4 N3 N, ^+ ~' h; M& Q) e; R8 J

' a4 L. Q5 R1 W& j: u- n   # Z" D1 B" [: |) A4 j7 g( U

( I: t( R" \, u7 p0 Sprint dir(st)2 Y" z& `( d$ Y( N( A; U
6 `$ o$ n" G' s! w* Y4 a. h0 r
print        
: f% v7 D- }2 u
8 R9 s. U# m3 `dump(st)# \( H1 o' Q, [5 x( b
- K) A# z" H9 m- M$ a" v
# print
; c; T, t/ n5 i* q, G1 a
  d) v: ~5 j7 m: T5 M9 \& ]   
3 |- M! y7 m( U& v
& t' c9 H% S& O% h, ^fp = open(file)7 l; p; P+ h3 L3 Z/ H5 ^

/ @. x& d8 Q6 p! x7 H  {st = os.fstat(fp.fileno())
- f8 ^5 ]7 [8 n5 u7 B
( Q2 j# q' n) n6 f. z+ Z$ aprint "fstat", file+ K, m+ [1 s8 m& t9 D3 [

  f: O5 M- z0 d- Adump(st)7 c) T! f2 W' b, b1 A
' \7 d; v9 Q9 f+ P
   
1 w4 X  K4 u- G) G. _- y- n, N! B5 f- {3 {; ]3 C! |4 y
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):
+ \% A0 i/ h  E# p9 X+ I2 z$ @' j0 x! p) C- ^/ C
os.fstat(path/file)# T2 Z0 K8 L' |, }; D

! L% K' [' F: TReturn 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
' L/ {, c+ y1 x9 I可以放进文档当成附件下载的!
6 ?4 S$ b( H. v8 Q  n# O# b! I
很好的建议哈




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