13,009
回編集
771行目: | 771行目: | ||
== 条件に一致するファイル / ディレクトリの一覧の取得 == | == 条件に一致するファイル / ディレクトリの一覧の取得 == | ||
==== 同階層のファイル / ディレクトリの一覧の取得 ==== | |||
指定した条件に一致するファイルの一覧を取得する場合は、<code>glob</code>モジュールの<code>glob</code>関数を使用する。<br> | 指定した条件に一致するファイルの一覧を取得する場合は、<code>glob</code>モジュールの<code>glob</code>関数を使用する。<br> | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
861行目: | 862行目: | ||
# 出力 | # 出力 | ||
./test/ab?cd.txt | ./test/ab?cd.txt | ||
</syntaxhighlight> | |||
<br> | |||
==== 再帰的にファイル / ディレクトリの一覧の取得 ==== | |||
<code>glob</code>関数において、第2引数に<code>True</code>を指定することにより、特殊文字<code>**</code>が使用できる。<br> | |||
<br> | |||
パスの指定において、<code>**</code>を使用することにより、全てのファイルおよび0個以上のディレクトリとサブディレクトリにマッチする。<br> | |||
<syntaxhighlight lang="python"> | |||
glob.glob(pathname *, recursive = False) | |||
</syntaxhighlight> | |||
<br> | |||
例えば、パスを./**/*.txtと指定する場合、a.txtやmemo.txt等の同階層のディレクトリにあるファイルの他に、<br> | |||
./doc/b.txtや./html/back/2020/report.txt等のサブディレクトリにあるファイルも対象となる。<br> | |||
<br> | |||
以下の例では、サブディレクトリを再帰的に検索して条件に一致するファイルを取得している。<br> | |||
<syntaxhighlight lang="python"> | |||
import glob | |||
for name in glob.glob('./test/**/*.txt', recursive = True): | |||
print(name) | |||
# 出力 | |||
./test/book.txt | |||
./test/cup.txt | |||
./test/pen.txt | |||
./test/doc/sample.txt | |||
./test/back/2020.txt | |||
./test/back/old/2017.txt | |||
</syntaxhighlight> | |||
<br> | |||
==== 条件に一致するファイルやディレクトリをイテレータとして取得 ==== | |||
<code>glob</code>関数は、条件に一致する全てのファイルやディレクトリが含まれるリストが取得できるが、<br> | |||
<code>iglob</code>関数は、条件に一致するファイルやディレクトリに順にアクセスできるイテレータを取得することができる。<br> | |||
<syntaxhighlight lang="python"> | |||
glob.iglob(pathname *, recursive = False) | |||
</syntaxhighlight> | |||
<br> | |||
多くのファイルやディレクトリが一致するような場合は、全ての結果をまとめて取得するのではなく、逐一取得するイテレータを使用する方が実行速度が早い。<br> | |||
<syntaxhighlight lang="python"> | |||
import glob | |||
for name in glob.iglob('./test/**/*.txt', recursive=True): | |||
print(name) | |||
# 出力 | |||
./test\book.txt | |||
./test\cup.txt | |||
./test\pen.txt | |||
./test\back\2019.txt | |||
./test\back\2020.txt | |||
./test\back\old\2017.txt | |||
</syntaxhighlight> | |||
<br><br> | |||
== 条件に一致するファイル / ディレクトリの一覧の取得 (pathlibモジュールの使用) == | |||
==== 同階層のファイル / ディレクトリの一覧の取得 (pathlibモジュールの使用) ==== | |||
指定した条件に一致するファイルやディレクトリの一覧を取得する場合、pathlibモジュールにあるPathクラスのglobメソッドを使用する。<br> | |||
<syntaxhighlight lang="python"> | |||
Path.glob(pattern) | |||
</syntaxhighlight> | |||
<br> | |||
Pathクラスのglobメソッドは、パスが示すディレクトリおよびそのサブディレクトリに含まれるファイルやディレクトリにおいて、<br> | |||
第1引数に指定したパターンとマッチするファイルやディレクトリへ順にアクセスできるイテレータを取得する。<br> | |||
この時、パターンには以下の特殊文字を指定することができる。<br> | |||
* : 0文字以上の任意の文字 | |||
? : 1文字の任意の文字 | |||
[abc] : 括弧の中のいずれかの文字 | |||
<br> | |||
<code>*</code>は、0文字以上の任意の文字とマッチする。<br> | |||
例えば、*.txtと指定する場合、a.txtやmemo.txt等の"0文字以上の任意の文字列" + ".txt"に一致するファイルおよびディレクトリの一覧を取得する。<br> | |||
<br> | |||
以下の例では、末尾が.jpgのファイルおよびディレクトリの一覧を取得している。<br> | |||
<syntaxhighlight lang="python"> | |||
import pathlib | |||
p = pathlib.Path('./test') | |||
for name in p.glob('*.txt'): | |||
print(name) | |||
# 出力 | |||
./test/a.jpg | |||
./test/cup.jpg | |||
./test/pen.jpg | |||
</syntaxhighlight> | |||
<br> | |||
以下の例では、bから始まるファイルおよびディレクトリの一覧を取得している。<br> | |||
<syntaxhighlight lang="python"> | |||
import pathlib | |||
p = pathlib.Path('./test') | |||
for name in p.glob('b*'): | |||
print(name) | |||
# 出力 | |||
./test/back.txt | |||
./test/book.png | |||
</syntaxhighlight> | |||
<br> | |||
<code>?</code>は、1文字の任意の文字とマッチする。<br> | |||
例えば、?.txtと指定する場合、a.txtやc.txt等の"1文字の任意の文字" + ".txt"と一致するファイルやディレクトリの一覧を取得する。<br> | |||
2文字以上のabc.txt等にはマッチしない。<br> | |||
<br> | |||
以下の例では、3文字の任意の文字で始まり、末尾が.txtのファイルおよびディレクトリの一覧を取得している。<br> | |||
<syntaxhighlight lang="python"> | |||
import pathlib | |||
p = pathlib.Path('./test') | |||
for name in p.glob('???.txt'): | |||
print(name) | |||
# 出力 | |||
./test/cup.txt | |||
./test/pen.txt | |||
</syntaxhighlight> | |||
<br> | |||
<code>[]</code>は、括弧の中に記述した文字のいずれか1文字とマッチする。<br> | |||
例えば、199[789].txtと指定する場合、1997.txt、1998.txt、1999.txtと一致するファイルやディレクトリの一覧を取得する。<br> | |||
1文字ではない19978.txt等にはマッチしない。<br> | |||
<br> | |||
また、[3-6]や[a-e]等のようにハイフンを記述することにより、文字の範囲を指定することができる。<br> | |||
[3-6]は[3456]と等価、[a-e]は[abcde]と等価である。<br> | |||
<br> | |||
以下の例では、最初にaからeまでの文字で始まり、末尾が.txtのファイルおよびディレクトリの一覧を取得している、<br> | |||
さらに、fからzまでの文字で始まり、末尾が.txtのファイルおよびディレクトリの一覧を取得している。<br> | |||
<syntaxhighlight lang="python"> | |||
import pathlib | |||
p = pathlib.Path('./test') | |||
for name in p.glob('[a-e]*.txt'): | |||
print(name) | |||
for name in p.glob('[c-z]*.txt'): | |||
print(name) | |||
# 出力 | |||
./test/book.txt | |||
./test/environment.txt | |||
./test/flavor.txt | |||
./test/pen.txt | |||
</syntaxhighlight> | |||
<br> | |||
<code>Path</code>クラスの<code>glob</code>メソッドにおいて、特殊文字である<code>*</code>、<code>?</code>、<code>[]</code>をを単なる文字として扱う場合は、<code>[]</code>で囲んで記述する。<br> | |||
例えば、?を文字として扱う場合は、[?]と記述する。<br> | |||
<br> | |||
==== 再帰的にファイル / ディレクトリの一覧の取得 (pathlibモジュールの使用) ==== | |||
<code>Path</code>クラスの<code>glob</code>メソッドにおいて、パスの指定時に**を使用することにより、全てのファイルおよび0個以上のディレクトリとサブディレクトリにマッチする。<br> | |||
これは、<code>glob</code>モジュールの<code>glob</code>関数とは異なり、<code>Path</code>クラスの<code>glob</code>メソッドはデフォルトでサブディレクトリを再帰的に検索する。<br> | |||
<br> | |||
例えば、パスを./**/*.txtと指定する場合、a.txtやmemo.txt等の同階層のディレクトリにあるファイルの他に、<br> | |||
./doc/b.txtや./html/back/2020/report.txt等のサブディレクトリにあるファイルも対象となる。<br> | |||
<br> | |||
以下の例では、サブディレクトリを再帰的に検索して条件に一致するファイルを取得している。<br> | |||
<syntaxhighlight lang="python"> | |||
import pathlib | |||
p = pathlib.Path('./test') | |||
for name in p.glob('**/*.txt'): | |||
print(name) | |||
# 出力 | |||
./test\book.txt | |||
./test\cup.txt | |||
./test\pen.txt | |||
./test\back\2020.txt | |||
./test\back\old\2017.txt | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> |