( l; u! l: ~6 |6 D; q4 z$ y/ \& G3 Tlistdir 函数返回给定目录下的所有文件(包括目录) * }5 |6 v8 o2 b / `2 \; k$ k5 s. j4 l# j \ 2 v9 B7 E" z* h8 m2 s6 Q( d! ?# K* O ^( ?; _2 ^8 A
import os V; ]7 Z3 N7 }" x8 n6 u( N+ e- L
! d5 m) U7 |; k3 _( g- c* F
for file in os.listdir("c:\qtest"): % M* t3 {9 [2 h* N2 f* ~' Z 0 d. N: h2 o* S' q; h/ Vprint file 5 ?6 G! T/ y% ^ t' U& d- w . W0 V' l' `2 z , {2 U. m, N" o: ]3 Y. b7 i9 O0 ]0 Q4 o9 V( M& D
getdir 获取当前目录 & J. l5 y4 P2 J3 J 1 [9 o; D4 L1 k/ xchdir 改变当前路径 2 {5 D/ j- y" y0 I: ] * g$ z" h. d7 m, N$ R0 N - ~2 m x7 [ p0 i, t
; l+ N5 b* Y: Y. C6 s `9 l$ H5 a7 S
cwd = os.getcwd()2 B# S7 i/ O- [, z, c7 x7 _5 w5 x
" X: x9 C+ c+ [( e. y+ G
print "1", cwd) G) t# \- G6 [' P
- z0 Z% D8 n7 h
# go down0 l$ M/ D$ @, ?$ p! [
% m/ i6 T3 f4 K
os.chdir("c:\qtest") / R" s6 T6 K1 t% b, p2 I0 G; Z- s! h/ w; {1 B4 @% z$ ^# F9 o
print "2", os.getcwd() 5 J" n2 ], _# k- x * X a3 l7 t5 i# go back up: B9 a9 |! O- S+ ^ F
$ g, W; J L9 {6 E+ ~: j8 t* V0 wos.chdir(os.pardir)#返回当前目录的父目录 9 I( {3 m) I1 {- P1 I, B * m4 l& ~2 n6 N8 t& \print "3", os.getcwd()% T" O3 T/ ?7 |4 Y0 I6 L# {8 Z9 n
: u/ Y. P) j* ^: `" ?& o
& u; V9 G1 b! b' D9 n5 L & v0 E% t& c! \makedirs removedirs 生成和删除目录/ O; k1 D5 j: d( e4 o
% f9 k4 W! P$ g4 x" o8 ~makedirs可以生成多层递归目录, removedirs可以删除多层递归的空目录,若目录中有文件则无法删除7 |2 I1 C4 v+ s$ u$ y, G2 R
. J5 Q9 Q, `5 Z; T
+ u6 L9 p4 E* b4 c, m( @
4 b# `3 `" m6 x
import os! o6 z0 ?/ W$ b3 l3 Q
' N; K; g+ ]1 Ios.makedirs("c:\\test\\multiple\\levels") 7 u |" @4 }: s) z4 U9 l0 h+ H& X1 _( \6 b- ^1 k
fp = open("c:\\test\\multiple\\levels\\file.txt", "w") % g p3 `& C' e. \ L- ]3 w+ Z6 n( Q5 K, q# ~6 z: H
fp.write("inspector praline")* s9 B, X+ F- g" t$ G
$ h7 u% a" h& M H4 {
fp.close(). V4 x" X7 H, H, F) N. V
5 k, V. y0 }( X, N, m# remove the file& D7 J7 Y9 b4 M, q4 i3 P
- e# [) g- t4 ?
os.remove("c:\\test\\multiple\\levels\\file.txt") 6 S, H3 Z9 X5 B, o7 H4 e! B+ F2 N) f
# and all empty directories above it9 L: x. A8 x2 ^, W3 {
8 ~/ l2 f: }. C: L
os.removedirs("c:\\test\\multiple\\levels")6 z+ B$ J! F& H
+ G; b$ {2 p0 b6 l) y# G. m - Y3 k" q, j+ u; b3 {( s7 M' e2 v8 u6 J: u1 o! t0 j1 J8 M
mkdir 和 rmdir只能处理单级目录操作. 8 v2 J' ~( z' Z1 r" ?% _; }9 g ! {4 M- ?* H# j8 I% h若要删除非空目录, 可使用 shutil模块中的rmtree函数 2 i/ K# S) p' Y; Z* e# C4 X6 A/ I
9 Q' `* F# S* q" f( _ v6 v; p% O7 P# M( B7 F
3. 文件属性的操作 + w. v l- `" l4 X+ j5 F ; j6 v% P2 M2 p( u. U0 `+ z7 M3 dimport os 9 m; e4 \" v* @( _1 H1 P: E4 |; G0 y! f# x1 ]$ N7 _
import time+ ~& ?- ^5 d* ?+ x% D3 O. _* M4 T
0 A$ o3 G2 c" I/ ]6 i% s" Kfile = 'c:\qtest\editor.pyc'/ _. z7 y, `( U% C$ X
5 Q6 }) x# B# b7 v) w$ P " q7 u( Z* A1 H" J- I8 A) E) t" c
" l: N' W' |2 _9 j- X3 Rst = os.stat(file) ( Q9 k" w2 W: m& p" L$ ^8 Q - H( O& p u I+ F: W* \print "state", file1 [$ K h4 h3 }7 N# P
: Z9 w1 a2 K1 r+ R 9 H3 _* ?! C. k3 j; C: D( b( ?* A9 F- B1 n. V
def dump(st): 1 f+ Y& o9 a- E' _6 s) I( T) j. |& k: |/ h8 k" Z9 [: H, S
mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st - q9 J6 e9 t7 y7 o2 g5 b+ f' C! L& N( S0 J" ^
print "- size:", size, "bytes"3 ]0 F0 \6 H# r9 F# Z/ N; }7 r
( t5 s8 o2 K: B! l6 R" P* nprint "- owner:", uid, gid 1 n5 O. H) h' z1 S , ?( T5 t; D/ j eprint "- created:", time.ctime(ctime) f0 K0 u- o3 a! D$ @5 g7 y' ~
; b* Y1 @- z! X& L6 c+ [
print "- last accessed:", time.ctime(atime); ]" C* o/ q7 a1 j; b
, ~1 l/ E }1 Y
print "- last modified:", time.ctime(mtime) $ F8 C4 r j9 X; L3 I& s9 _& e, h% @8 G: u- r% `+ `0 V6 l' d
print "- mode:", oct(mode)6 C3 L8 S0 m$ e; c: n
X E" V. V2 ]: a2 L# ?+ j
print "- inode/dev:", ino, dev& p$ }3 c* ^1 U9 w+ a2 l
, @: L) w1 w5 l" U: g" m4 y ) i) x$ o: }2 m8 C+ {
' v+ j2 b; {) B# y, [' B. k1 k' D
print dir(st) ) d# z' w+ _8 F: r9 F8 g, m ; @) R2 E' z2 i( M" sprint 2 J: e& ^9 e: z7 S/ Z
7 P/ E0 l& M+ X0 k2 T7 N+ Xdump(st) " u7 S2 M" C2 h5 P$ Z$ a6 M6 g . Y% e8 E# h u+ P# k; Q: e# print9 F. E6 y$ O. [! p. u: [" R: `
9 R- J. F$ H/ m" H+ @
n c2 c- b$ R- X# I t c8 j0 M W1 r1 k
fp = open(file) 1 u6 o* E, u8 p/ F5 ^1 @( T% C4 ]$ S$ L" ~# F
st = os.fstat(fp.fileno()) ) T: f. {; y1 J; I9 E - \( o/ G' n! V( ?/ c, `. wprint "fstat", file - a* s, y6 ~5 F 9 r6 G5 [ T$ j6 ~ `dump(st) - u/ J# X# {# A& W4 S0 A$ E/ K2 h3 L+ x1 o$ e$ i
$ ]# A: q' A. d0 H ( ~1 g- K! ]; n1 Oremark: 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):+ t2 i/ [; v' l! y
' @7 m L- T" `8 K% i6 dos.fstat(path/file)5 ^7 \4 I4 s/ L# L% }' I
) K I7 U4 {) V$ X4 E1 I$ E8 yReturn status for file descriptor fd, like stat().