- 2008-08-09 (土) 0:00
- Python
あるディレクトリから特定のファイルを検索したい場合、探索対象ディレクトリ内のファイルを全て取得する必要があります。今回は、引数にディレクトリを指すパスを指定することによって、そのディレクトリの内容を取得する関数を2つ示します。
ソースコード
# coding: Shift_JIS
import os # osモジュールのインポート
# os.listdir('パス')
# 指定したパス内の全てのファイルとディレクトリを要素とするリストを返す
files = os.listdir('C:\Python25\')
for file in files:
print file
実行結果の一例
DLLs Doc include Lib libs LICENSE.txt lxml-wininst.log NEWS.txt PIL-wininst.log pysqlite-wininst.log pysqlite2-doc python.exe pythonw.exe README.txt Removelxml.exe RemovePIL.exe Removepysqlite.exe Scripts tcl Tools w9xpopen.exe
ワイルドカードでリスティング対象を指定
globモジュールのglob()関数の引数内にワイルドカード「*」を含めることが出来ます。これによって、より手軽に目的のファイルを探索することが出来ます。
ソースコード
# coding: Shift_JIS
import glob
# パス内の全ての"指定パス+ファイル名"と"指定パス+ディレクトリ名"を要素とするリストを返す
files = glob.glob('C:\Python25\*.*') # ワイルドカードが使用可能
for file in files:
print file
os.listdir()と異なり、glob.glob()は取得したファイル文字列には先頭に探査ディレクトリ(引数)のパスが付いています。
実行結果の一例
C:Python25LICENSE.txt
C:Python25lxml-wininst.log
C:Python25NEWS.txt
C:Python25PIL-wininst.log
C:Python25pysqlite-wininst.log
C:Python25python.exe
C:Python25pythonw.exe
C:Python25README.txt
C:Python25Removelxml.exe
C:Python25RemovePIL.exe
C:Python25Removepysqlite.exe
C:Python25w9xpopen.exe
リファレンス
関連記事
- Rで統計: 作業ディレクトリの設定と確認 - setwd()、getwd()関数
- Python: CSVファイルの読み込み - csv.readerオブジェクト
- Python: テキストファイルの読み込み - read()、readlines()、readline()メソッド
- ユーザー管理に関するLinuxコマンド
- AIR: テキストファイルを非同期に読み込む - openAsync()、readMultiByte()
- Newer: ActionScript: マウスをイベントリスナーに登録
- Older: サイトURLの変更のお知らせ
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://www.yukun.info/blog/2008/08/python-directory-listdir-glob.html/trackback
- Listed below are links to weblogs that reference
- Python: 指定したパスのディレクトリ中のファイル一覧を出力 from Yukun's Blog