os.walk() 方法
os.walk() 方法用于通過在目錄樹種游走輸出在目錄中的文件名,向上或者向下。語法格式如下:
os.walk(top, topdown=True, onerror=None, followlinks=False)
# top -- 將要遍歷的目錄地址。根目錄下的每一個文件夾(包含它自己), 產生3-元組 (dirpath, dirnames, filenames)【文件夾路徑, 文件夾名字, 文件名】。
# topdown --可選,為True或者沒有指定, 一個目錄的的3-元組將比它的任何子文件夾的3-元組先產生 (目錄自上而下)。如果topdown為 False, 一個目錄的3-元組將比它的任何子文件夾的3-元組后產生 (目錄自下而上)。
# onerror -- 可選,是一個函數; 它調用時有一個參數, 一個OSError實例。報告這錯誤后,繼續walk,或者拋出exception終止walk。
# followlinks -- 設置為 true,則通過軟鏈接訪問目錄。
os.walk 的返回值是一個生成器(generator),也就是說我們需要不斷的遍歷它,來獲得所有的內容。每次遍歷的對象都是返回的是一個三元組(root, dirs, files):
- root 所指的是當前正在遍歷的這個文件夾的本身的地址
- dirs 是一個 list ,內容是該文件夾中所有的目錄的名字(不包括子目錄)
- files 同樣是 list , 內容是該文件夾中所有的文件(不包括子目錄)
實例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))
執行以上程序輸出結果為:
./.bash_logout
./amrood.tar.gz
./.emacs
./httpd.conf
./www.tar.gz
./mysql.tar.gz
./test.py
./.bashrc
./.bash_history
./.bash_profile
./tmp
./tmp/test.py
os.path
實例:
# -*- coding: UTF-8 -*-
import os
print os.path.abspath("d:\\new\\test.txt") #返回絕對路徑
>>> d:\new\test.txt
print os.path.basename("d:\\new\\test.txt") #返回文件名
>>> test.txt
print os.path.dirname("d:\\new\\test.txt") #返回文件路徑
>>> d:\new
print os.path.exists("d:\\new")
>>> True
print os.path.lexists("d:\\new")
>>> True
print os.path.expanduser("d:\\new\\text.txt")
>>> d:\new\text.txt
print os.path.getatime("d:\\new") #最后訪問時間
>>> 1322235096.47
print os.path.getmtime("d:\\new") #最后修改路徑時間
>>> 1322235096.47
print os.path.getctime("d:\\new") #創建時間
>>> 1321610018.9
print os.path.getsize("d:\\new\\") #獲取路徑的大小 字節為單位
>>> 16384
print os.path.isabs("d:\\") #判斷是否為絕對路徑
>>> True
print os.path.isfile("d:\\new\\hello.txt") #判斷路徑是否為文件
>>> True
print os.path.isdir("d:\\new") #判斷路徑是否為目錄
>>> True
print os.path.islink("d:\\new\\hello.txt") #判斷路徑是否為鏈接
>>> False
print os.path.join("d:\\new","hello.txt") #把目錄和文件名合成一個路徑
>>> d:\new\hello.txt
print os.path.normcase("d:\\new\\hello.txt")#規范path字符串形式
>>> d:\new\hello.txt
print os.path.realpath("d:\\new\\hello.txt")#返回path的真實路徑
>>> hello.txt
print os.path.split("d:\\new\\hello.txt") #分離文件名 返回元組
>>> ('d:\\new', 'hello.txt')
print os.path.splitdrive("d:\\new\\hello.txt") #分離磁盤驅動器與路徑 返回元組
>>> ('d:', '\\new\\hello.txt')
print os.path.splitext("d:\\new\\hello.txt") #分離擴展名 返回元組
>>> ('d:\\new\\hello', '.txt')
os.stat() 方法
os.stat() 方法用于在給定的路徑上執行一個系統 stat 的調用。
os.stat(path)
# path--指定路徑
返回值:
- st_mode: inode 保護模式
- st_ino: inode 節點號。
- st_dev: inode 駐留的設備。
- st_nlink: inode 的鏈接數。
- st_uid: 所有者的用戶ID。
- st_gid: 所有者的組ID。
- st_size: 普通文件以字節為單位的大小;包含等待某些特殊文件的數據。
- st_atime: 上次訪問的時間。
- st_mtime: 最后一次修改的時間。
- st_ctime: 由操作系統報告的"ctime"。在某些系統上(如Unix)是最新的元數據更改的時間,在其它系統上(如Windows)是創建時間(詳細信息參見平臺的文檔)。
實例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys
# 顯示文件 "a2.py" 信息
statinfo = os.stat('a2.py')
print statinfo
"""
輸出:
posix.stat_result(st_mode=33188, st_ino=3940649674337682L, st_dev=277923425L, st
_nlink=1, st_uid=400, st_gid=401, st_size=335L, st_atime=1330498089, st_mtime=13
30498089, st_ctime=1330498089)
"""