7 {& c) p0 m: U5 y! G6 L+ s' Q$ u1 A0 V7 zmkdir 和 rmdir只能处理单级目录操作. # {8 r, Q& _; _, v+ g% I5 r% A' N$ i 2 u4 h( Z2 s' B+ g4 N5 C若要删除非空目录, 可使用 shutil模块中的rmtree函数 e$ t. c5 Q! e6 @
- M/ `/ w7 B7 S( W8 O# z% Z
/ B! M- R) X; m) f' U& O7 D: t' Y9 _1 Q+ j% \
3. 文件属性的操作 ( c8 v, i& d( p; I; u! n - S3 T) Z% t7 D& A7 zimport os: v6 M; R8 A% Y' J& W: W. X: X
9 B4 G% k' R3 A7 [/ `8 |4 N
import time ; n; f% A' O$ r9 n5 _& j0 f# B* P9 b3 M- D9 v I3 V+ n! d. e
file = 'c:\qtest\editor.pyc', k- A( T: H2 M S) Q% ?9 V: p! b
# [1 o9 Z+ f5 K+ {4 u3 D8 X7 f H ; u. C7 ~$ F0 w5 e
* T+ I7 H5 P% _& t& T' yst = os.stat(file)* A3 F' Y0 z! u
* z1 z/ u( x! u& G
print "state", file " |2 K7 `( Q, t6 \3 Y3 U% Z5 }. j/ r4 l5 [+ U5 w( ?) c
0 j% [. g* p( R' y$ G1 n: w; t q$ [1 D: a- y. _2 T# V" [
def dump(st):/ ]6 l6 G" [- e# ]
6 S+ t' [: [, b& Y& A$ H
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st 3 x' s. q; y( e) @/ o5 w$ w s! L! G
print "- size:", size, "bytes" 8 u$ k4 I1 X. P% R& h" \ & M& \1 ~0 w& E. {print "- owner:", uid, gid* k. _0 @5 ^- q0 t( f1 X* [
2 n& I% n7 C8 s. R
print "- created:", time.ctime(ctime) ; n8 R6 N9 X$ l2 R! T: _4 V7 m0 X t6 b+ a
print "- last accessed:", time.ctime(atime); w. i4 B9 x- U# U
6 y2 G- S+ r8 ]- @8 e7 w2 b' pprint "- last modified:", time.ctime(mtime)) {6 Y4 I, g q: B& S5 Y4 D1 m# w7 \
0 V; H3 D( h% p3 }# t: R. v) e
print "- mode:", oct(mode)8 p/ g7 u; f" \( K, g9 n
# x/ X: ]; F' M6 ?
print "- inode/dev:", ino, dev 8 k5 W }- z L4 q( n& z 0 [: y& G" J7 ~/ n 4 S% ~. Q, } m% x& Z }( f# F' u9 n7 \9 ]& jprint dir(st)" C8 V7 W: Z2 W/ n7 I, l: h
" z( R: ~, N# C# Y1 `" u9 ^2 U7 R/ A
print ; ?+ q: e; k5 H6 v2 `3 x ; h9 u& F% `' b4 e# x, ~6 Kdump(st), G! f5 G# @# C J
$ l- I- M1 h5 Q
# print- F" H- x' s, I: H
$ X: s+ d, v; }, C! A / G0 A4 x* A$ i c5 z. g/ _
" g. _1 T1 D' P. ^3 |fp = open(file) - u3 [7 _: \( G- b! g/ O0 B0 I: O, k/ L H' S/ V4 r& D+ N9 d
st = os.fstat(fp.fileno())# Y- S/ M" e, J7 [ x
' ~% T/ b. J+ \5 \6 m7 L; d) ?print "fstat", file 1 G4 O4 _2 G- }6 a$ ^$ s" k . g+ V W, ]4 Ldump(st)" }% O8 w# t7 U; Z- c
, V3 K3 t/ A& [- C8 n : D. n2 |& S% h" J/ r2 R2 {& h
# n- U9 V" [" @( Eremark: 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): ; A$ h; G3 j( q" ^0 i4 X* P$ \ ( Q! I# L3 d( a" los.fstat(path/file): g$ ^6 ?( Z. r: c: A
( T0 Q% p% F* a. A7 ~; { K$ ?
Return 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 : H* @8 o, z# M2 s w可以放进文档当成附件下载的!