数学建模社区-数学中国

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

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

* T" `$ |# q: R
0 A) `' S  B* z0 h# y4 Kos 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作
9 U+ P4 Y/ c4 r$ ~7 X! o: e
& X+ Q& ^8 r/ t3 I% q   
1 P  a) W" F6 ~" |" e, D5 ~1 P0 E# Z) Q
1.文件操作
$ I  D- Q) c- s/ p/ ~' i& l3 [9 @7 D( l7 X; M$ E
build-in函数 open 实现文件创建, 打开, 修改文件的操作
' J1 J, P% v% e! K* _! s$ Y) \0 t) ^8 e( X/ h" q" W

( B- W% e  B5 G
- O) z1 |- g- Iimport os% W7 v4 I! ?! v* M
1 b, l/ n) e% T9 [+ n
import string
, n" @# @; m3 U& A  X" V# ~* z7 |
: {( f' @7 K' A7 s4 |) [   7 @9 w  ?; J" R* q6 v7 x

, @7 q4 \# \! l! Fdef replace(file, search_for, replace_with):
3 Z, H& s7 v9 H6 a  n: [( l, H1 r1 c( y  G! U3 X  f9 o: e% O
# replace strings in a text file
/ q& @. i0 }3 s/ @0 y, u% ?, r$ D+ Z; J+ u, O
   $ H. @5 P' r# l" H

' q3 K" E. @# d# p" R) aback = os.path.splitext(file)[0] + ".bak"  `6 W+ h* Y8 ~& h9 z1 y

8 k8 i* R- z. h. Z" j- w% Atemp = os.path.splitext(file)[0] + ".tmp"
9 ]- p- y# y/ v" b' i1 V5 z7 L# {. T3 Q7 k, H  f* c7 q$ c8 U4 A
   % g& T: f0 ]( ~5 Z1 T
& G6 c* ^" C% W( w: g
try:: ~( |! D/ j' ]. u

5 P( N8 P4 M6 f# remove old temp file, if any3 T7 ^% O# X: [+ A

. X2 Y! Q6 u. los.remove(temp)
2 L& V1 H/ n' P7 O/ I/ V# X/ P
7 ]7 q$ x/ y  v% x  J1 i3 q& uexcept os.error:
6 O# Z5 D( @  H, R; W$ V9 ]+ J5 J4 S  G. J) m' _
pass
" A2 W, H4 ]; g
1 y" R( B8 a0 ?3 [% B   
$ e; D6 U/ I0 W
7 F7 H  _  Y6 a, c( E1 ?1 pfi = open(file) #: ^( z% T( o+ ?, n7 u5 ]
* l! O" g! h4 a9 g
fo = open(temp, "w")        #
' S+ |6 \" m$ Y- G1 i: {$ \3 K6 i2 S/ x& V, _# n+ d
   
' S$ l; ~' z* Q& }6 ]  r, z& }$ ~, D2 |
for s in fi.readlines():0 N6 H0 J# r. t; n
' L" G, O7 s: y2 J9 E
fo.write(string.replace(s, search_for, replace_with)), q* R2 e+ f& @# G/ \8 n9 B
5 \2 C0 f3 n9 I+ R* e0 _
   4 Y9 s3 [6 \3 ~
+ h! r1 Y. s% V4 g* E6 D3 n4 d* z
fi.close()
7 X  Y4 }7 G  `& D/ h2 |
6 \5 A  l8 B  k! [fo.close(), A: \% ?& ?" o( d! F9 H
4 s  T% V. U6 C4 A9 [7 T, E" l
   
2 G4 n3 ^, @7 A* e0 P4 X& h$ \5 A1 h  v/ L& }1 s4 r6 L- p
try:
# p1 L+ ^8 g! N  x4 L9 y9 S
  f" E8 N. k4 X% |& s, |# remove old backup file, if any
4 m: [4 ?) \, |/ ]- v  [2 |/ L: b$ u, o
os.remove(back)1 u9 C3 U1 O- A/ u& X

' l! l2 B5 E  b# X1 c; @except os.error:
) q5 |7 z9 X. v+ n6 c1 i
! p6 z: Z* u# ?pass
0 M$ A' s, _' m) v; v! a+ B
0 ^8 N$ h2 `( c9 w# t& Q   9 \" q7 f# |  O& j. ]

2 I* ]' Q2 K& o3 U' U8 A# rename original to backup...
: R$ ]6 c0 H1 C- I/ h4 }5 h& b! i1 \$ b$ B7 I2 V
os.rename(file, back)
+ m' d! P) b7 M( T; @+ r- }; A
/ v" o/ ^: ~! s$ h8 N: N) G   
0 ?- l1 ^- f, A' Y$ X2 w: Z; q, z5 E
! S; `' X' ^8 e' X# ...and temporary to original
/ g* y: X9 Y' T# Y3 l! @* j/ Q, l" S2 M3 Y5 L5 W; B
os.rename(temp, file)
4 e4 ?0 Z% H. u$ E
0 A- u% o7 {4 G4 t& S" z3 g+ o   5 _: ?2 x. m6 R3 L; E
. Z% \# O9 z* T7 G2 b/ R& f) _" U$ r' ?
# try it out!
# `1 C; m5 @/ ]# |7 a4 ?4 `3 z8 g, Z0 N' ~& P
   2 p% ]: z' l5 ^' C. H

9 r  p, J+ s& u& R. Cfile = "c:\samples\sample.txt"
1 f! v1 L0 V3 U; {' G7 t; f* c1 q& f2 E
   
5 K; I% C5 T. f9 J# h9 R! A9 e; n$ }9 G9 ^5 j& t
replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena
" [& w) C% \, e8 @6 W* S# M4 ?8 q, Q. W
replace(file, "tjena", "hello")8 K+ i% V6 v: H! g' V
# J* B2 R! B' a' r
   
/ h! g+ W! d: L. [) {$ P3 C
  x+ |+ F* K8 A' ~2. 目录操作
3 |/ J+ L5 x; e$ O
8 X7 m& E: l- r2 Ros 模块包含了许多对目录操作的函数
" s4 |* l8 o* t9 C' d9 b6 `" F$ K1 a, d! X& u) W! y
listdir 函数返回给定目录下的所有文件(包括目录)
( w' Q$ K- S& z! a3 H
1 v/ ?/ B# I3 U; a- \   
0 C+ z& _: a0 m3 L6 M: U  W7 R% j3 n1 C9 P( [: a. J# v
import os. M0 \4 i" W. j& l% T
: i! u9 R+ q, C' H! k8 L0 d
for file in os.listdir("c:\qtest"):
8 Y5 M' ?! d. ]5 O$ V$ ?8 N' \7 K- s8 A4 t
print file1 a% ?3 N; d; b. e3 X- w
& X4 K0 C7 [8 O7 v/ l
   : S) t% o- \( E; K  f; M  w4 B

% K( R  P) h* ]+ _$ fgetdir 获取当前目录
) u% T: `) \* A+ `/ i
: B1 E2 U. |! R% X8 P/ e5 ]4 u5 x4 qchdir 改变当前路径
/ U, ^# F: `7 v4 U# b- ]$ W" U  q+ t) {% |  G
   " E: s0 k" P  x
: b9 [; \/ I: _1 w4 B; S
cwd = os.getcwd()/ a+ S5 V# d& [% _/ e* q- m
' e& x# Z; h/ J/ Q
print "1", cwd; d) W+ D" d0 z8 |( W7 D2 Z
+ u. O% f2 J+ Q9 P' `* q
# go down, z. |! ?4 V. U, z, y% n" h

$ ?( l) {3 {* }" Q6 Hos.chdir("c:\qtest")- M0 ]; b. Y+ \, _! U" R8 D

8 D0 H, B- H5 x. C+ D3 e7 ]print "2", os.getcwd()
8 ~/ B3 T% `. }& w5 F0 F/ t5 m) a$ ]; l% c) ^
# go back up9 S  e. \1 j; N2 S* h
; {* x6 U1 @. ^& N
os.chdir(os.pardir)#返回当前目录的父目录
5 M- O7 P( G0 c5 k$ y. i& S6 t
0 C1 C; Q+ v2 k1 j* u0 B' a9 `print "3", os.getcwd()' |  h) ~5 o; Y8 X, q

/ J9 I0 [7 [# i! h6 B   
% m, U, Y6 K( c  V- v$ `5 B+ `% k/ A: w# p# L! Y
makedirs removedirs 生成和删除目录4 e: [( N! P6 w! U$ I# N
# O! a% q) |% u; Y' D6 S
makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除$ J/ ~3 f  e# f2 F- V% d
+ Z% d: P% B, i, L
   3 K1 t9 ]3 m5 g: N2 G; N
. A+ d8 d: A4 v; T* L7 {6 [4 W
import os
. H" ?( a. l5 p- C: F. u* \9 k; t/ s  h) ?
os.makedirs("c:\\test\\multiple\\levels")
: R3 W8 [0 {1 t$ `/ e1 i; n5 M- w' T+ C- t4 E. u: v: b. B- B
fp = open("c:\\test\\multiple\\levels\\file.txt", "w")
( e+ f8 ?8 g5 g( ]3 r
- @' f7 K2 T1 f( ofp.write("inspector praline"), F2 {7 [; G; Y( S& I

9 V( \( i- p" }1 U, T* u3 V$ |. R* G/ xfp.close(), ?* O1 A7 i2 v* o: B% r6 H. |1 a
4 c& T! r2 j! u  }5 z
# remove the file
( H8 i8 p& x3 b7 U3 G+ i5 \. h
& Q4 e! x  i  o+ z4 @6 ]8 P. Kos.remove("c:\\test\\multiple\\levels\\file.txt")) ~* B0 q$ f/ I" ]! q
$ q/ m/ e2 g. `& p; I/ S$ \
# and all empty directories above it5 v( w9 [& E" F( B+ g, C$ l

: i; E4 ?5 k; y" Aos.removedirs("c:\\test\\multiple\\levels"), B, n; c  b2 H# ?+ @3 b+ @' X- @

+ U- t4 l6 Y2 U( T# L5 P+ F3 K0 m   
) O# d* m7 F( e" b6 N+ e) V+ x4 E
. [" ~, |, x) Omkdir 和 rmdir只能处理单级目录操作.9 s0 N0 ^# m' P

' E0 n6 {3 _$ W3 b7 j6 U若要删除非空目录, 可使用 shutil模块中的rmtree函数$ @; r  w, n" f' Q

  I4 @& t' K- ]) c$ s6 x* N   $ q( |8 v3 k: _  c2 v

3 P0 a4 k# a" k4 M/ Q( F' y+ Z! K: K- w( O# }3. 文件属性的操作6 G- A( |0 m, G* @
) b6 A% K- c& Z. o; b% @
import os. v; z; s" ?4 l

2 k4 L" n# D( C, r" z5 ?! Vimport time$ W2 p3 e" n( x
$ G7 n( s0 c9 h8 I9 r- m+ d3 Q
file = 'c:\qtest\editor.pyc'" }, T( ?& x" J$ U; E, i- _
; o! P; {0 @8 E) E
   
  b* q3 z/ d5 }8 R: `! \. e( [1 ^# g* e0 r2 ^, A$ R
st = os.stat(file)
# }; `& ^$ N& p) i* F- A) t2 G
' d1 z; |$ A( |7 j2 @; q& W, c. x% p2 sprint "state", file/ m$ p! C+ U% _! A* u  |6 h8 A5 H' `1 u
, N7 x( N8 a, c6 x- q, l
   8 x. D  ?7 K( h0 p
0 {9 u0 a! k! u' z
def dump(st):
; |0 x- B; x; O7 b; [$ D  A% N# u  X& O5 \+ w1 X7 V/ |( I* u+ n
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st) E6 n7 Y  Q5 V: c- S& P

  t* t8 v2 v+ t' x; `print "- size:", size, "bytes"
3 {9 P# P& Y" G7 o4 a6 ?1 j
* x& i( z! Z7 C# V: s) Z" a  e0 Mprint "- owner:", uid, gid
! P1 Z/ \) g1 W+ O  ~* f7 }+ n1 m* B0 w4 i
print "- created:", time.ctime(ctime). ^6 v" s. `$ t, j9 J$ B3 [

/ g/ V% }1 M0 uprint "- last accessed:", time.ctime(atime)
' }7 |( S4 K2 A* L7 q, f
# U5 C. b: y" ~3 Y& u3 C% U! Tprint "- last modified:", time.ctime(mtime)$ G4 t  t2 u5 N  {
. O0 v! ^6 z% c) ^/ `; d
print "- mode:", oct(mode)0 ~0 a8 _* F, ?: p( h" |

# C" D3 I* u( A' @* m2 ^7 a) [  [print "- inode/dev:", ino, dev
8 Q! w! @( ]. N1 v$ P* \7 }
; v2 k+ X! x3 F  P$ m( E( o- Z5 e9 M   
, v5 X2 L  v4 w) C, A( H
" p' Z- G2 E; Y" g! ~2 }print dir(st)& g1 g& w+ c8 w
. C+ n/ Z  |2 T1 I* i  v0 u" z# i3 q
print        
7 Q7 `' |4 v% X1 F9 j8 l) L5 \; b+ Q
, r9 E/ l* b: }1 S6 \dump(st)* }, i" l- n  _8 |
8 s& I- R" f* M, C  w7 S; a5 x
# print
9 M+ W1 H1 I) m( a' u- ]& d' c) Q$ e5 o* B, y2 a! ^: ~
   
# G5 n( J( Q4 |* i/ b! o0 K$ Z. z3 F+ g7 F
fp = open(file)
/ W1 ?' v* C4 q# Y1 I% N" f+ h3 C7 H- x8 C6 g0 [
st = os.fstat(fp.fileno())
* m, Q; w( a: f: K8 Y1 R8 ^, Y" g2 w5 N
print "fstat", file
- d+ Z' r0 D8 Z8 U3 I, j) ^$ {+ P; E2 ]
dump(st)/ J+ W% P3 @& ^" S

2 s2 D: N0 J- O9 \: x. X6 R   * Q) r+ s! h7 F

/ j3 v' g7 ?6 L" fremark: 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):
, L7 i, P9 G7 t7 q7 U
# w; U$ J2 W: Y6 A7 F4 a8 F8 kos.fstat(path/file)
$ u9 Y$ j* V  x& Y" ^$ K( K8 Q4 R
# L0 {  `- V2 RReturn 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
0 u! }8 m" d9 f0 I! Y! f可以放进文档当成附件下载的!
; Q) A+ x7 K" c- Q3 J
很好的建议哈




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