数学建模社区-数学中国

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

作者: Seawind2012    时间: 2012-7-10 13:49
标题: Python OS模块学习(一)剪切
6 T. W- D5 O# d- X' O, }
  H* o8 }0 {( {8 R! l
os 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作% i; b) v3 x- d; f
" K/ g' G! F7 ^& _! z. ?8 {
   
% z% ]8 S( T7 A; L" ?8 @  @4 `9 c6 I' N# {# x5 v
1.文件操作
5 D4 w9 `9 q, q1 N
* G5 }, n7 C3 zbuild-in函数 open 实现文件创建, 打开, 修改文件的操作+ f8 Q+ w+ T9 i& J5 E, _- J

( N2 K5 Z4 r6 B7 R% z7 I
. H; r9 l+ y0 B$ h; y6 h# y' s1 ?7 n' {; ~
import os/ Z% u( x9 G4 U3 A. `8 m, O& ?/ U
. g& M' g  e8 X; K
import string
2 l: l0 b$ M6 M6 L; C
0 u. n7 J9 c. z# ?   - P% b" Y; ^! ^# Y! E( ?8 Z( H

7 O  @6 w: w4 e! P4 ]7 e& wdef replace(file, search_for, replace_with):# ^8 S6 p# R) A" T8 c, B0 c& W8 ~3 i

! h0 j" P7 P* u! k# replace strings in a text file
+ u  A# m$ e+ B1 v! L
, Q5 T7 }( [3 @% \  X" Q   
/ h  D7 T- G1 z5 L( |
0 P3 z. L2 `+ ?- B( D, i; kback = os.path.splitext(file)[0] + ".bak"
4 R) B; H: X4 t4 L" f" F' Y" w- G0 K* H/ Q
temp = os.path.splitext(file)[0] + ".tmp": J/ K* o: m( G" \* ~- C
+ y) _" d" D( `9 }! J
   % x% d4 K: c( b+ |1 H- u* P. t

+ I! p$ j8 q; M8 V! e+ @1 p( Ztry:
1 R" Y. Y6 l9 F! k. D
3 @( E9 x. x* r  G5 v# remove old temp file, if any/ ^) }6 d  N$ k" H' A) h: \( ^

7 l. }1 F9 d( I% @; z2 X/ Wos.remove(temp)
; Y% D3 Z( a$ k+ k
2 M$ t8 }$ i4 @6 _# g# _: Eexcept os.error:
; K2 ]( V' _  t* ~, u
9 V! S& l0 [0 d$ s& r( K4 Spass# t( M0 v/ O( Y* {5 M
7 q- g  a8 g1 G. p! Q8 k. o
   
) ]# T2 l& A. \- P+ O- {9 x' i5 n) U- o+ C5 P! {/ u
fi = open(file) #
8 m' D4 U: L- `! ]% S7 {7 Q7 c0 K0 K
fo = open(temp, "w")        #% E% x0 A' }2 Q; {; t

" b. e! Z1 ]0 S! z( @- @4 F   
  b+ t$ C/ W9 @' g1 G, I
$ I7 t% J) x# f7 E' M& Sfor s in fi.readlines():
7 d- R' P( w0 i5 l* @4 w3 D) e
1 B7 D& Q0 ?$ W% z* b5 Hfo.write(string.replace(s, search_for, replace_with))0 A8 L( i2 d% d4 y; D! S

2 I/ ~3 K/ B1 o% Q( t* a   ( I5 M$ U  f5 o
- b: S. {  z- R3 d- i; K, }
fi.close()% y1 x3 k: ^9 X
: Q' D8 x4 v9 v0 C4 c
fo.close()
% k+ H0 Z' p4 i( @; }9 I; y8 R, E% b0 P+ ^. x+ _/ y* h; l) r
   
4 c8 ^3 L3 d! ^$ y9 s7 V5 K: X. p% W, S# f1 _! \
try:- M8 s5 L# k1 |; t2 R2 s
  o- q" X* X3 O# _- Q& y
# remove old backup file, if any' Y/ a0 @1 S4 H5 H

/ K6 W/ X! ~) X& \" c( Eos.remove(back)
2 w/ Y' q- _* t% r1 v- U3 [/ j5 X; S
except os.error:" B: s$ g+ C/ ?* G

) B1 F) k7 r& {9 B+ |! ]pass7 U, R$ _) Y0 O( w3 z3 v- W* D
2 x: u; {! V! z$ q; Z! v# \
   
* S4 ~9 z, H2 h# k* @, J4 \% k* t; w' H' T, ?6 W
# rename original to backup...* X. R- R; T* T3 g- Q
/ M4 |( I  _0 l, Y
os.rename(file, back)
1 g9 J- V+ Q( ]$ w
- e7 J) U  _  |: T1 X* r' o1 \   ' v  u" t5 b2 x

) e- Q. @, u( p9 l# ...and temporary to original
4 P9 n$ K$ @/ F6 ^
  m$ p1 o! v, P7 |5 W& s  _7 sos.rename(temp, file)+ g& @! z7 X3 r. y
# v, \% q) k1 A! {0 d7 a  e
   
  f5 x$ x2 m2 A! ?: ~% c7 Y
- ?5 y) ?& s  P3 _0 \1 @9 o' x# f# try it out!+ S/ O& R! k9 o; }

& A. f- j# c; w1 u  `( E   
3 s% C; Y4 P2 |( d5 V
6 V' W+ v( S# nfile = "c:\samples\sample.txt"
& i+ |! f# k0 R5 P6 N2 V" j. U, B5 G" T5 o
   
' f9 N1 H1 c* e! F5 K1 q3 d- ?: V/ f* \( c4 g
replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena& S, ?0 [1 ?6 R. S/ g4 @4 n

! g: U! I; T( v& greplace(file, "tjena", "hello")
; h) h/ Y: J: \' p& ^7 _. F* O( s0 {: T; a- F
   ' S( V5 Q2 A( x. ^
  F3 f6 c' G9 K% \! }
2. 目录操作
( t: |1 }/ {8 ~% u
# r8 Y5 B/ v3 b) Gos 模块包含了许多对目录操作的函数
2 G" k6 k7 q% E& J. U. ]5 f% _
  f: f, o  i; K7 xlistdir 函数返回给定目录下的所有文件(包括目录)
* W, {8 {4 K5 f7 c$ e! b& N. D) e. ~- x4 v- V! ]0 ]
   
3 R9 V7 N' w8 F: A. U  \9 C, H: P' S4 M
$ d7 G; j, f; k/ Kimport os. p& U$ `/ ^& _- i. o; m$ h
& [# K% v9 ?( A8 f9 @
for file in os.listdir("c:\qtest"):$ U; _; _1 t, U; s
0 M( j: L7 y. y( `: f8 K4 x
print file# F9 a0 N) g( @2 B( n2 O, \0 a

1 @  ~! g, y& }  M! G4 j! y   
$ p* A2 B. L; l$ a: N+ J
  Y3 L8 y4 w8 igetdir 获取当前目录
, F8 @  e4 g8 Q; H/ g! W
; j: _9 D+ ^2 g* w9 ichdir 改变当前路径
3 S  S5 E  W6 D0 y1 \: T: c
8 z& f! `( V6 F) O4 W   4 C& X& Y& F: e. `' v
! p, j  E" M0 q8 y1 ~0 c
cwd = os.getcwd()* [: |6 A5 R8 J7 i$ s
( l/ I& ?+ N: i: G. N' W4 ?# Q
print "1", cwd8 n: K0 }3 v1 r9 a8 `

2 e9 }; S, D# ]# go down+ c$ a4 R' f$ c% d3 b+ h
5 G( y1 ?+ {/ h8 E5 y) {. {8 w
os.chdir("c:\qtest")1 g) J2 V* d+ C* F& T

0 d% q' k; W( u/ Xprint "2", os.getcwd()
, z9 y! v1 x2 S- o; I2 l& @% q4 `" [7 {3 a; {4 o0 {
# go back up
2 x6 S2 w# r, c! I3 s, q* Y
3 W% w: ?; O# k7 ^os.chdir(os.pardir)#返回当前目录的父目录
! c* R, H$ b* V0 w
$ m9 i  y6 H$ G8 Z$ C( W0 Lprint "3", os.getcwd()5 r0 f9 ~$ F# p, t
2 l+ o' A: f! B, w+ g1 {( ]" {
   
" i! I2 ]! i7 R; d% @0 y8 ^! r" z( Z
' l  O: m* k0 d- A0 i; V) D8 fmakedirs removedirs 生成和删除目录! O& @4 {4 j6 N- h+ E% D
8 r; a. {" m. P! l* S  o+ k
makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除; U4 S# }, s. z* v- f4 I& w

% [2 x% d5 _) p, B! O, A1 N- p   0 `/ o4 W/ i7 X% C+ S. `0 C; h

6 a5 H6 R* `7 a& wimport os% ]* A' `: c" H7 Q2 [' \

0 W) _3 g- R$ L5 w, g) Jos.makedirs("c:\\test\\multiple\\levels")3 p- `# y' p/ m  @" g1 z

1 D+ R' N/ z3 |- Rfp = open("c:\\test\\multiple\\levels\\file.txt", "w")
; V+ ]8 V1 ?$ B* t9 ^4 l/ t7 @3 f' v) e% @- q
fp.write("inspector praline")+ S5 @. Z# w3 @+ V# D
2 E8 F2 y2 w9 }2 @2 ?4 B
fp.close()1 `3 A8 K0 _: `

! S" v* @7 X- S3 [+ g4 I. T# remove the file
8 s' A* g0 x$ T
6 i; ]3 s7 x4 d8 g' B* g4 X; qos.remove("c:\\test\\multiple\\levels\\file.txt")
# @; t9 w& z  Y  o% u. L; ?" I+ v* l6 B9 p8 L& I) ~0 f
# and all empty directories above it
/ q0 c! `+ R# |: k3 N
: d6 [' U0 x* X$ {os.removedirs("c:\\test\\multiple\\levels")7 {7 o1 v, x1 q0 |0 E6 E
% A6 V4 l4 s& j" S
   
% V9 K" O6 \7 v: r5 b
& j* c  Z) r5 t! t* A, Ymkdir 和 rmdir只能处理单级目录操作.
, L/ t" ~: V4 s7 e
! v  a* W+ c  y" n' g' k8 C若要删除非空目录, 可使用 shutil模块中的rmtree函数
1 t, _0 _" Z% c' z, g: }) C# U& S3 |4 Q: T  B
   6 `7 v" C; }3 ?

  i& a7 n& O# S& q. H/ ^9 ?3. 文件属性的操作
2 p  _/ [! ]0 G  i4 ^; C2 O
( p4 Q0 q' m: ~$ F& Limport os
* E( ^8 F  R9 N- {
7 |: u5 K5 J+ a' ]0 Y; nimport time3 M) E( P$ L$ `' m

- o& _$ \% s0 u: ifile = 'c:\qtest\editor.pyc'  C6 o) P% c5 _. {" ~
  g% S) x# @! n# B8 R
   ; Z4 P* ?" A& a2 ?$ n  |$ S; K

" t+ H: K1 ~" Yst = os.stat(file)
2 b$ T" g0 T" W" C
6 M* f, P1 R+ U7 }print "state", file5 ^  p( E# ?, i2 n% I

" d  t. I  \  H  e, k" T4 @   
1 T& Y1 S' `/ R4 ^% r  a( O# E) h+ O  f4 \3 H( @: D0 e
def dump(st):
5 E( P% _$ \+ \1 S: G& u  J5 t! \& m1 _1 W
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
& T5 `/ j: P, t5 a( s
9 |$ O9 L3 K* p$ c4 Z! A0 x' a  gprint "- size:", size, "bytes"% g+ B% d9 T2 x, G. I0 T& H

" C! q* |- i2 dprint "- owner:", uid, gid( P+ _8 s, Z7 O/ o/ v  F# Q3 S+ n2 Y
) [% ^5 A. y7 Q  ]- _; E# R; b, L9 K
print "- created:", time.ctime(ctime)
" N: H6 U" c& c  b* u8 ]4 }- c. a
print "- last accessed:", time.ctime(atime)
4 T  J$ j0 f  S* r
1 E* ?9 `! u% \/ `print "- last modified:", time.ctime(mtime)
% z5 ?6 _0 I9 H5 t1 N( c# ?  c' a# q
print "- mode:", oct(mode)
( @" d2 o+ f% V
5 K; M5 V, g8 Q. T) Mprint "- inode/dev:", ino, dev! V  ?" |1 ^- B( a
& u# H5 _- P. o* z- X: B
   
4 ^% G: Z8 v. U" B
/ @* M* p' k: bprint dir(st)
+ P. _% f; G8 r; X
3 w; u8 a1 C, P' wprint        
, [# a. E+ E3 s- _7 Q* \$ d1 g6 B- E
dump(st)3 z/ O; G' Y- V4 x
4 P  c) F+ @+ k; R8 J* F
# print5 e: y4 @9 t+ e' C

2 j7 B  x2 j$ }( V   
- N7 `4 Z- m' L  o( H' d; l# u: B( m+ m& a' ~, R
fp = open(file)4 H8 \. R7 _- a, j: e! l

/ j# j: W' S5 m" y, y# A! Pst = os.fstat(fp.fileno())
, |: B0 n7 ]8 ]$ }
% n4 e$ H0 g, V) }, N1 y' qprint "fstat", file- w8 ?3 {& u4 C( i' I$ }
. w+ b  L7 ], |- U. A: a  k4 k
dump(st)
: F, m/ {$ h  ~
8 u9 V4 F6 O8 R/ C6 P& \: L3 k   
7 `3 X5 i! A8 Q* V  A6 r- M! p: a9 c6 Q2 v
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):
. c% t* r3 D5 |& v5 t) \6 X! O
" V* I( K: R' W- cos.fstat(path/file)
: q% k' w' M) ?3 _7 s
3 _2 i  B' o- IReturn 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 8 g5 r* u: j1 {' q0 L; R( ?
可以放进文档当成附件下载的!

# E; j) A/ c  d# q很好的建议哈




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