数学建模社区-数学中国

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

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

% k& e; ?3 e. L) I1 i
5 ?, N8 L4 h7 }3 d/ |6 K1 H( los 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作% s8 V" d  B9 _! K, d# `. `
* K1 }$ W4 j1 k, n- x+ Z+ c7 t
   ; u, d( M) z4 w$ C( S$ f
2 E6 {; P6 n( I$ A
1.文件操作0 w2 X( m* _- I5 z# `

7 \& a  c4 a7 }8 sbuild-in函数 open 实现文件创建, 打开, 修改文件的操作
5 u; C8 g! p+ a. t: v0 N1 v# p3 x. }  e5 D

. E3 T' |" U1 u0 G4 L
: m0 r. D7 V6 x; ^8 Z/ ~; Simport os' N9 F6 N" W2 q

0 y% {+ J5 C$ v4 \& Ximport string) ~  v0 ~- N) |

/ M4 }# b" @3 k: {- J4 M   
% z% w: _/ P. J$ b) J# Q4 l+ q2 D( h4 C
def replace(file, search_for, replace_with):8 e( {2 S( g1 Z( s4 k
. o8 u; p' h5 ^6 L; W, ?
# replace strings in a text file9 T+ B, p/ Q7 l4 }9 ~+ @) {" p1 ?5 o! \9 l% a

3 ~. i: V% R. z3 Y$ s% A& x) U( u6 w4 O   
/ c& F0 t1 L1 N* `, R' p- l8 T! M
back = os.path.splitext(file)[0] + ".bak"
& |1 y+ Q7 S! a7 B3 y  y( l& e# |0 ?4 Z
temp = os.path.splitext(file)[0] + ".tmp"7 |* Y2 M7 X* j

) z/ Q# J( |' `+ a$ n0 D" K   
) P& r1 q6 C( @: B' v6 z3 t
1 R+ \: n1 K0 o1 W, c* f4 itry:
! p+ g' T- A# f! j% f$ Q, |5 J+ |" e6 l7 P7 |& \# u( L% u' ]& s5 D- `6 {* s
# remove old temp file, if any0 k" }' Y5 e) A" G$ a6 E" _
6 K& ]1 ~+ c- k2 a
os.remove(temp)
/ V9 L4 [( Q" f& N( N1 q% ?* }/ ~- C- z1 o8 V% A7 \' Y
except os.error:
) y+ Z/ B* ^2 M( {" T! w6 O  t# `' W6 Z- ]( K1 r
pass* A! H9 M5 d5 P3 U* G8 `6 y

$ b, Y4 o( B' B* h   # D/ R0 f7 ?* M! S

6 K/ \; {6 Q( \4 U3 Ifi = open(file) #
$ E$ \8 h# Z. o+ B$ L+ G7 ]7 U0 {3 h& R5 x: ^
fo = open(temp, "w")        #
6 m" x( K( A& b7 y' e9 y! R0 x$ Z
   . g; V$ M7 ^" _4 O/ ?1 ~$ V; ?% N

* t, }7 n' ~: ^: C8 m1 n- b5 dfor s in fi.readlines():7 I0 M2 O. T9 [: `9 Q  t

, H+ [+ d. X. |4 t& jfo.write(string.replace(s, search_for, replace_with))
0 h1 \' G1 k) n5 f
% s4 \3 A& X2 C4 L; j; c3 Y, @; `  @   
/ b- L( n4 [0 O$ z; P/ Q3 q
( z3 k. }. r. |$ A  y7 Mfi.close()
. @; r7 \) w: A- B% K, Z& R* v; v( [6 u. H  @/ H; M
fo.close()
/ E/ c' y( x& T* h# Q/ W% E9 o7 C) p' q
   
' H0 ]  l( y4 B  j
, z: i) b% K4 g& Ftry:: q8 l4 i* i; E* }; T

- M/ C7 X" f$ e9 o" n. k( H# remove old backup file, if any/ A0 ?; X5 R, x6 i
( o& y' Y* ]: n6 C0 U$ G) d- [1 }% k
os.remove(back)
% |; j4 B9 L' t
' m$ e* f9 k6 c5 [( fexcept os.error:
# x; @4 v6 e5 e/ K: K3 a6 G" B$ ]8 p: f
pass# B! E+ O8 p; i# f: L8 m2 {
; n% x  E, w; r* o% K3 v, C: B
   ) b7 b8 C4 [# W" k& S4 B: b4 Z, a0 b

% @) a+ m" z% @# rename original to backup...0 w' h9 M% r7 K; `& n0 _) G: N! V  d1 N% ?
( E% v! E$ q  J/ U) x/ B4 E
os.rename(file, back)
2 j4 x5 z% b% ^* s6 `; h: O3 R, ^6 C* B7 m
   1 [* ?2 i8 d% l8 ?" x# |
3 x: W: l& X4 [' K0 f
# ...and temporary to original+ K6 Y6 I3 f4 {# y; }: ]9 ~  G

1 [8 j) l1 `& F9 ~" t% gos.rename(temp, file)2 A& C7 Z+ W0 _! J. X
% `& i7 b- B! h1 l) o
   - m. t/ z4 t) }$ M6 w( B. ~: M
! m& g3 I/ L2 D+ |- Y1 l* a1 ~. |) ^" t
# try it out!
& @( R; x' ^' R& v
+ V" E! i, ]1 n! x) C   
5 r% a  _# q. u4 }5 ?) k% H
9 o; Y- v1 C0 v3 M! ~file = "c:\samples\sample.txt"
# W/ D& u3 }) H+ J
9 d- r3 G* E- A9 J# a+ q   
; h( B$ b+ p2 m) w' M; A( p* @: K) Q; N, x* i; L: ~, ]. f
replace(file, "hello", "tjena")# search for the string 'hello' and replace with 'tjena  z5 J$ H9 C7 W2 C- H/ l
% ^) W7 \& D5 }* L
replace(file, "tjena", "hello"). U( q4 `0 [6 v( L: D; c
6 L& C( K1 H* }' x: P2 b1 b  J
   - _: O$ X' S1 [4 }' f( E
, K: U+ Y- n4 \# h& A& }, }
2. 目录操作# L; |/ _: M! V0 b
3 E" G' U& P5 e9 T' t2 w+ \
os 模块包含了许多对目录操作的函数
+ z& e4 g7 s1 w! |. `$ W. a: p2 r9 z; G5 k. R
listdir 函数返回给定目录下的所有文件(包括目录), N; X' t1 w$ z5 Q

) t0 N, w; y" K& x5 W6 e0 a5 K: T   
4 {+ ~7 d, `( C/ }: e9 f9 ^* Q$ T  F) [; |
import os
* {. i6 B  n3 k3 Q5 f% l7 ]8 c$ b
8 A" M0 j* H, }8 vfor file in os.listdir("c:\qtest"):
* j& I3 B/ u8 P( Q; `2 b: T, f4 V' R) b* b5 u
print file
! [. q2 H- A! R; M! p  E4 z! s6 `1 R2 _1 `/ c: K
   ( W" B8 \8 R; @9 e4 e/ l3 L7 B
! W  K- e- v- D% c
getdir 获取当前目录
/ x$ `& L8 a" _0 t; U/ I$ ~! l/ t. C% L" K, Y  [1 e( }
chdir 改变当前路径
- O( O2 J: K6 n0 y; S9 B& ^0 R! ]( b, A( k$ @/ G+ ^, p4 e4 C. |2 O
   * i# n8 p" T, R( N8 ]  Y! ]6 n. H( ^4 u

' w5 Q4 X" Y; S( f5 i( a! v8 Ecwd = os.getcwd()
+ d. k& |% `8 ]+ {$ z4 `7 u: w9 v% f1 n+ a1 v1 i* g( G
print "1", cwd
2 t$ _, s) s# `0 [6 p8 ~( q( s
8 i0 O; ]1 e) n: q. v# go down
! [3 w# t! _% Q: }
& k2 Y( I4 V5 s4 F% O, ros.chdir("c:\qtest")' v. C" {' ]; h; C8 h3 r8 c6 ?

/ n0 _* f/ N0 f  Y. U$ Rprint "2", os.getcwd()5 `: C3 b4 e  D, A4 p$ U
# [  z6 [# f2 H' F! e' {
# go back up+ X  b& X1 E1 }' A5 o. f# A5 q7 M
. X8 T; f) _! p- A) U3 G
os.chdir(os.pardir)#返回当前目录的父目录2 l3 W0 _3 ^, m8 j' ^
7 ~+ a" T- E" e& d2 M2 c
print "3", os.getcwd()9 g: f1 H3 E! J! {+ F8 c3 \* i

9 o* O6 O' Q- ]" U/ y' K5 q   * t6 S) Y" o0 {; }! a! M/ I3 U0 J

) r& c: B" J* kmakedirs removedirs 生成和删除目录, x8 k' D7 R% T' S6 T) @
$ k: T: \; ?1 \: g7 Y
makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除: \* H- l; Q7 x. ^: ^

6 y: ^, g9 K; C; A5 h5 F   ( p3 Q( O, o' H3 W# e" Q
0 H5 `6 s, a# q
import os. s  p& v- U4 ~0 o6 f$ D; R. V
4 N4 g) k$ c2 N# R5 g/ A5 u
os.makedirs("c:\\test\\multiple\\levels")
/ P' c& M/ q: ^% V( Y8 P0 P# b, Q3 B0 O& `
fp = open("c:\\test\\multiple\\levels\\file.txt", "w")
5 h- |7 F4 O1 O$ H* Q: i' ~  t6 Z! E& v- |1 w* X2 s0 t; W
fp.write("inspector praline")( y' ?  r9 g9 G% i4 O$ t
- a; K. h' \  w' m2 F2 X$ ?/ n
fp.close()! A$ ]8 V+ e4 Z' p- ^
% ^8 g2 n, ?$ a& q
# remove the file
2 c& e0 |$ d3 s9 w: R; E+ D$ y: U' J7 A: j% V4 p1 N8 @/ b, _
os.remove("c:\\test\\multiple\\levels\\file.txt"), V( Z6 v7 J/ C6 g9 w% Q. o. e+ r
! r) H6 ]+ [, w5 I6 G5 m
# and all empty directories above it8 U+ }, a) U7 Y+ h% i
- c  S6 T, [8 U5 _( `4 H: _2 |+ u
os.removedirs("c:\\test\\multiple\\levels")
& i5 X# q5 h' O( X3 a6 D) c; Y8 h2 n% |- t3 N
   
" ~" ~6 I6 G( c( }! p
3 k$ w4 j+ z" D/ y. |- u) hmkdir 和 rmdir只能处理单级目录操作.
1 E" k1 c- t: Q2 z+ E  j" i% D: r  D% k% E
若要删除非空目录, 可使用 shutil模块中的rmtree函数) n/ l' Z! a4 v- D0 q

; A/ |$ C, v7 J$ Y* @. S. q! r   : D% S7 x9 A  k/ s$ _+ d# z8 j% c

7 R9 _/ b" K/ v' v3. 文件属性的操作: ]  E0 X: {' u1 O

& |# t- K% A3 t8 _4 M) ~import os$ [# m" @* k: M" q# m2 R
6 @, n  i  I, _: q7 g5 T
import time4 o  v/ F8 \5 W" `8 f) W' k3 V4 w$ a* k% U

: M' S& p0 W5 C% |4 S' x9 V" pfile = 'c:\qtest\editor.pyc'
9 Z+ C* V+ L" R5 L0 B9 R# N1 h: i4 ]& C0 \
   ) ^" n5 f$ E! x) l! K! x, N- m% [
! }5 b6 O  D; {
st = os.stat(file)0 ?, g- x+ K3 p, V
% w# s- ~# F! f) o
print "state", file
, P2 z2 r; J9 D2 g: X0 x; T6 i6 Y0 ~' c9 B2 g0 f8 Y2 @* y
   
, _5 R9 Z' O# Y: J! g
- z/ o7 H5 _! O7 i% |9 ?def dump(st):
5 e% ^& A+ ]5 |0 G% a& V! i' i) o8 x/ Q# M
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st3 i$ g# C9 C0 N
' Z4 @3 N0 X" r8 h# @
print "- size:", size, "bytes"
, f# q( A7 u* o5 g8 g% {" E4 D# ]* N) C, i3 M" ]3 t
print "- owner:", uid, gid- H$ [& w5 ]' a

# U4 n4 V$ j9 i2 g! [2 p" L8 [: Nprint "- created:", time.ctime(ctime)
5 \: R& {" l0 g7 j4 h) u4 u8 p9 h+ `3 C/ s
print "- last accessed:", time.ctime(atime)  F, }; X' v3 V' Z  s8 f, T9 Q

& W; |0 y7 U( w* hprint "- last modified:", time.ctime(mtime)
$ N3 {# y4 T! Z7 I9 V, r. O$ S* K" k& y: E$ e8 t. \
print "- mode:", oct(mode)3 E6 e2 S9 i- K0 a; M

8 m  V! a. g: R% ]print "- inode/dev:", ino, dev
' V7 G5 ~* X$ j- Y4 _/ U9 j  j! j. l+ i: R% Y9 X
   ' c7 X% H7 n  m, Y+ [! _8 [

* l9 Z( W) U9 N) r: s. N- Aprint dir(st)4 G7 E' m. z/ a+ D

9 [- ]: l/ E& e2 C! l/ M" oprint        
  m) f6 z4 t8 O4 Q2 S$ O
$ E+ ]" n6 ]  [, w7 y4 g* H# Jdump(st)
( G) g+ w, s2 k: Q7 q; p
: E! z$ @$ v, a7 J: w# print% {) y2 d! c7 K7 `; K/ l
1 J* o8 \! ^; n5 g: n8 f' O
   ) s9 ?2 ?0 L' B. ^* N9 U
- J, ~) K7 e* y8 C% B, @
fp = open(file)* i( v& ]$ U3 `5 h7 H
# h& N  R) o: v: }$ |6 u, i
st = os.fstat(fp.fileno())5 j# Z& L4 N) y, T- t% P) p0 @* T

0 z2 H& W3 f4 }6 K0 H6 H: e$ _4 y- Kprint "fstat", file6 w4 A, R; z$ |6 I
4 a2 U2 a5 p9 \
dump(st)& @2 G2 }7 R) q& G* p3 n& P

) Z6 M: q' z7 ?' h* M' o1 L: w' U   
+ n# x! J& n: \+ y, E6 k7 @3 ?; W. O2 B, Z: h8 Z( \
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):
7 u$ S3 F- n7 ]! N8 d/ M- S" X: `9 b: G" ~# _6 E
os.fstat(path/file)
5 n" k+ B" o/ M$ Q4 v
# S% n0 u- N& _* H0 NReturn 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 : e3 w. a+ u+ I* h( G
可以放进文档当成附件下载的!
7 D* T& l/ O: T1 j7 g, N+ Q
很好的建议哈




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