数学建模社区-数学中国

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

作者: Seawind2012    时间: 2012-7-10 13:49
标题: Python OS模块学习(一)剪切
" I! Y5 m$ X1 c9 K+ G6 c& `4 X

, l; k- I5 f# W  S* yos 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作6 V6 B$ L+ W# O3 Q
0 d/ b- j; j) |+ I7 E
   - U/ ~  Q' F3 b( K% u
: I% `( K! u7 n/ U3 p) M7 n
1.文件操作3 I' m+ t' X) k3 \

, N7 A! f/ [! ~) I: _build-in函数 open 实现文件创建, 打开, 修改文件的操作
4 _1 l% L9 i; D2 y' R# \% B7 ?. ^0 M; |7 Y2 R: j! b; A9 J
/ U' M, k3 ?& m& a; u" F6 D+ Z
: {1 Z8 b* @1 W1 ^4 p
import os, _! s& h. ]% t3 a" }8 R

1 e0 P* p' c1 q$ r$ wimport string! [+ ^; G9 i: ~% l4 s+ Q+ S

2 c* h  h* t; [   * j' A( k, S9 [; ?( q. V0 T8 N

/ U; r0 l# X, ?  _% fdef replace(file, search_for, replace_with):% T* K: b$ h; ~) p
$ K; h9 f* J0 {% a
# replace strings in a text file
. y+ p4 V; M" n6 {& c! U  t5 N2 x) B3 r2 F$ `
   
5 f3 `2 {/ h. E) m" D  O. b/ N4 |5 M' N* t! Y) r! r" G
back = os.path.splitext(file)[0] + ".bak"
" s! R' N: K9 l* ]! O! [( y
' J3 u6 H! }: p7 \, N# utemp = os.path.splitext(file)[0] + ".tmp"# S" G- x' M) b% w1 s0 ^
3 X& Z( Y$ D3 G; e; Y7 L
   
6 {1 q6 n# Y: F5 ^; I  {; Y3 W; t2 H2 T  v/ ]
try:
7 `* x8 e; q& O7 F5 s" O2 l. x  L9 y# R, w$ H0 E
# remove old temp file, if any1 W# @; B8 @/ o  b8 [
8 Z' l' a: X$ P7 [
os.remove(temp)- M( w0 T/ l! w$ [

" z/ K% A: s+ c2 `except os.error:* ^9 g1 j5 p. V" o$ e# E. }* {- W& x

6 h6 P) D' N  ~; E( W2 Vpass0 D" ?5 Z& c  l$ M

* J0 A2 d) \. E   7 I9 O" d9 `- v( r
- h3 F$ U+ b! r% e0 @
fi = open(file) #
5 W4 k  `3 k& n' }4 K4 Y7 P, L/ ?5 {9 f5 ]
fo = open(temp, "w")        #7 Q! E# m# R/ j( _
& w5 H+ n( Z- t8 L2 F
   $ e" U# d3 L2 [
2 K3 l" v" _  I% b  K  Z0 @5 d2 y
for s in fi.readlines():6 H8 }/ j; I' @) D- g$ `4 |

& U+ h) P( i2 D4 V$ Ufo.write(string.replace(s, search_for, replace_with))
- v  ~+ u" N: I" J6 _& l0 D5 o: A" _# Z0 v7 W$ p3 W, \
   
0 a1 e. z( l1 |7 i: S3 A9 ~1 P, W
fi.close()
/ m) [4 t2 V% V( B1 e7 j# h# v; l: L. L, _$ U2 I
fo.close()6 N7 H" _, g1 c  W' Y5 j+ M' A! b8 ?
. g5 t" E5 J/ f
   
! ~  ~  q! [1 L# t% \' _% F% h: O! U
try:
8 g3 C4 [% n" w; Z$ p! q2 ^+ D' p6 K* Q" z: c7 p4 S. V7 ]
# remove old backup file, if any+ S9 f- Z/ W5 d1 i1 D- U' Z

2 L4 }- A$ k+ h2 I  W5 sos.remove(back)/ j( l5 x; U! `+ w$ j3 u& x7 O  @* }) t
/ e+ C$ M( y3 Y; o4 Y+ t
except os.error:4 G! g# b* \) L$ |* D7 `& ?- I, j6 ]

! S3 Z5 I8 ~: H/ X/ H  {/ Mpass
4 x4 e/ M7 }) Z# i$ f6 G- [# H; p
: u# i- C; d/ [( B   
; s' T5 l( z  ~7 p3 e# A' x  K& i3 M
! D. p! g6 U! r2 R5 [4 c# rename original to backup...
4 E$ \" O: g+ G& A0 j! g1 Q# N( Y" h5 F8 t- f7 |- q& {
os.rename(file, back)
+ m4 M* K3 `+ k- K. U+ y0 S2 F8 E, U
   * t# {$ G" a/ z/ A8 s  c- k

( f; o. A4 l, u2 ^: b2 \# ...and temporary to original8 s, ]" m, Y, |
  \  _; k5 v/ x1 f" e
os.rename(temp, file)0 Z# u' r& a5 ]0 h, a' B
; ]; a2 b2 R! t
   
" ~7 T; A" E, r. _- w9 d3 x- V$ p3 o! C  N
# try it out!; w% R! ~# m/ [4 k6 K
! |- r* I& B! m2 q5 f, C
   & z! L, a$ I0 ]' u

. \2 Q+ o. o  l' T. m& [file = "c:\samples\sample.txt"
5 v3 R+ T" A- v8 r2 X, i) r, z- e2 @7 \) r
   0 [0 c' E: K9 @, I' j- t! j8 `

5 \9 ^$ J- @: q! R$ Mreplace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena) Z8 R# x7 y3 H% v1 Q" J

6 Y& U3 c0 V% Zreplace(file, "tjena", "hello")
' a/ n) N$ ?3 |6 c
6 h. f' p7 }/ H1 s! k0 J   % q: y% P: }9 [0 n7 ?% w

# p% U9 q% ^) C/ O. p2. 目录操作
1 s/ {( \6 j  s$ I1 S1 m4 {2 D; S. J, E& X1 ~" R: K
os 模块包含了许多对目录操作的函数
. M2 p8 q0 ], }. k
9 j! o$ O! r3 e# [4 f4 ~listdir 函数返回给定目录下的所有文件(包括目录)# I- L, m/ a5 t' h! p0 Z8 c9 }

/ ?7 P9 |. B7 m* I   
& t4 C* D0 G) k2 ^& U( ~) K# M, P" t4 O
import os
1 X' K8 ?3 ~  ^3 p8 C' k  O0 v3 D) E" s0 F* n7 i$ k0 N9 O" z
for file in os.listdir("c:\qtest"):5 _. i8 Z8 O. B" r* v  b+ d/ i. b& I
0 Y; u+ e, `8 _* @. {
print file
& C! d' A+ b" ]4 }* q: A0 [. s6 l$ ~5 q* E0 \  I
   6 h  {0 F  c, Y& }. e$ h! f
; k- _6 @$ D9 n' n' ~- q1 j  y
getdir 获取当前目录5 A/ U0 G. m- ~/ ~  _% _/ e3 k

' b7 Q/ V: Y& Ochdir 改变当前路径0 F. n5 S/ B. i% b) s% E9 d

& D0 O4 @3 C& P, J$ Q   * v7 ?4 @7 L! t6 k+ ^( Y' a

3 X- D: [9 p7 a5 Dcwd = os.getcwd()
" ^6 N( t# ~  ]! j
3 p9 P- H* Q% uprint "1", cwd. C, L+ R. V* g& I' l( g

' }& o& G: _6 I# go down
+ n( |; O( e( I# ^5 g3 i' R$ ~9 q5 Z' ~1 p
os.chdir("c:\qtest")7 L3 K* P8 h1 l# n0 z. o

8 _+ v4 H, }' v' |% ^. I: h* Vprint "2", os.getcwd()3 F+ @  s! N; m  A: B  Z7 e. n

- U7 W- M( _: h' P  A( z# go back up
6 j- {3 D+ h1 m  N' _6 g: ^% T/ |
os.chdir(os.pardir)#返回当前目录的父目录
; _; c% J9 p3 u6 c
8 K% x0 d, k' M% y8 E& Bprint "3", os.getcwd()
5 M) R7 W8 U2 T1 D( c1 S
) P: M; Z2 E  ^1 \   % O: l7 |% K- ^% C: N; H
% Y2 P$ V# V% _+ U- q, Q7 E5 N
makedirs removedirs 生成和删除目录4 @& S' E  r5 Y2 x6 i. m! ?

/ J9 ~% e. b0 A# J* Q- N' I8 h8 Hmakedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除& ]* @8 j$ P, g8 m( R

% Q7 X, R/ `, M1 i, z3 |   
7 Q! i, r( N( n1 [6 ?5 T/ g/ @1 U# h$ g: A- \( a( K0 I
import os
( r6 @5 J$ ]) H+ n- z  P" ^' [; d& r' `, T' H2 H' p
os.makedirs("c:\\test\\multiple\\levels")6 [7 W2 H0 k9 S. f( `+ T

- o+ \! d3 s* U/ C+ rfp = open("c:\\test\\multiple\\levels\\file.txt", "w")
* }$ P  O7 f9 J. r; q: |7 d9 S0 v
* k3 @8 j0 H7 U7 T6 O! |7 U; Zfp.write("inspector praline")
0 s" P" M- E9 X( {# a  t' I
" K8 u' i. _( ~5 T" c6 W9 ^fp.close()4 g% v* _: X, ?. x" |

" K) I* ^1 m6 E. H4 D1 _$ h8 _# remove the file
/ o* r2 d+ `' F3 P7 t4 F, j* V$ |0 r$ E% s
9 P& w7 n7 w2 z  ~+ u, t5 z; G% F8 ?os.remove("c:\\test\\multiple\\levels\\file.txt")
! R0 l- d& ]1 y' t. \7 K, B+ I" V  s* B
# and all empty directories above it- B5 R1 N2 f; a* z- F
) m. ^! X4 y" A+ P! k
os.removedirs("c:\\test\\multiple\\levels")8 @( u3 A6 ^. d

' Z6 z* T1 N) I% k  t   6 g' t) G" q0 S/ A, ?$ s3 |+ F

- x$ Z* ?1 {4 L! Xmkdir 和 rmdir只能处理单级目录操作.* M3 X+ k2 e  R$ S& w$ C

. U- C5 x- n& I若要删除非空目录, 可使用 shutil模块中的rmtree函数5 \/ m: x5 ~0 C4 g7 m0 v
) _& t, `# f6 S: P! k
   : t. l2 X2 g+ I5 T

# B7 F7 W: i& ^. q% R  u$ ]3. 文件属性的操作
1 n: M' A; z# H& h) v) s: n0 N: R, C8 ^8 j  L
import os! X4 Y% ]/ B+ o- G
, V5 ]0 S+ N2 O3 g( W
import time2 U! N% D( t6 h% v

6 f  P5 X& v" W  V- m3 p* V. Yfile = 'c:\qtest\editor.pyc'
, s4 R. N& @* d4 u; i* V; E% o1 K$ S, Z$ h9 L0 D
   # a7 D# S1 u2 C& I6 n: M+ n

" Y8 m" z' [' z5 {st = os.stat(file)2 Z. r1 m+ c3 J- m

. O8 x) I  o) \& j2 Xprint "state", file7 ]% q6 l5 [  X# w% A
5 M$ f5 O4 z9 A/ F2 [, b5 N4 B
   9 l1 z+ U# i! P2 e" u8 E
! b5 p, R2 i) `& A" h8 {
def dump(st):
! V7 h: C1 {2 A: o; a9 u3 ?6 N$ e: i* o' H$ l! H
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st+ H" J% Z5 P- g( C4 E5 U, r9 G

* X9 p' l% ?; J7 \# A- sprint "- size:", size, "bytes"7 h% O' U5 s# r0 e6 v1 @4 A% i

5 R. o' X$ T! [/ P7 J5 Cprint "- owner:", uid, gid
: X3 C6 U5 _0 h$ b, y: X, \2 u: w8 B
print "- created:", time.ctime(ctime)
5 Y4 @5 s. Q  r4 d
) Y% p* H$ j% W9 f. ]# nprint "- last accessed:", time.ctime(atime)3 s) h, e  e+ {! f; [

* o7 ]6 r0 _* D6 Kprint "- last modified:", time.ctime(mtime)
& @) L% k. b7 H- F2 Y: ]3 W: Q, ]* ^7 T
% g) S9 v" [% d7 Z  \7 W* _1 Fprint "- mode:", oct(mode)
. T) O  V6 \5 r5 e  G/ i# E1 S- p6 Q  c! P0 R* M
print "- inode/dev:", ino, dev
1 U* L  ?( e/ r1 Y; f4 p3 y: J* \" w( D( p, y7 D
   
7 `+ }. V' E. R& H: Z; G) u' u  J9 p3 S, V- \3 b# W
print dir(st)8 d1 N9 s' K9 C, ]8 T) Y

* L8 P, a  X* Qprint        
+ S6 w. j7 `6 q/ r  @3 _% F+ ]# {3 v3 Y( m3 e/ L7 y$ Q; Y! f& P
dump(st)6 `6 i* ^3 t' h$ R( X6 N# {
7 ^) ~8 q* @- |) }
# print+ O- D* @  p6 g3 \; `
) _6 A$ J3 q. R- n
   
. M- e' r' L8 `% `* W: M* I8 i, t# {8 |7 q; c  z  _/ d$ ]3 r6 L, o
fp = open(file)
- B% c3 m* a) t& u8 Y" {+ A- j- X2 U2 {' C' r5 r) p
st = os.fstat(fp.fileno())/ S+ v+ F' E7 S
. g  U' I" e( {9 Q" x# p
print "fstat", file
" P4 \# u" i* C$ _' H/ @8 o7 F: H
, [, a, g7 [7 G+ o, Edump(st)5 o8 t: ?2 s% \( L1 N, e. K
6 k0 g: q$ n3 D
   
  L- i: @& B  l" I
8 |. i( C$ T3 Qremark: 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):9 r, B" {+ o* G! c  Y# O1 C
2 G4 }$ y( q+ L
os.fstat(path/file)
1 O" P; z- K5 W# e
! T0 u4 @. n/ r( `) yReturn 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 1 t; ~1 S0 a% _! Y8 u
可以放进文档当成附件下载的!
* E6 I1 W5 N* B3 U
很好的建议哈




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