「Pythonの基礎 - ディレクトリ」の版間の差分

ナビゲーションに移動 検索に移動
491行目: 491行目:
  ./test\back\2020.txt
  ./test\back\2020.txt
  ./test\back\old\2017.txt
  ./test\back\old\2017.txt
</syntaxhighlight>
<br><br>
== ディレクトリに含まれるファイルとディレクトリの一覧の取得 ==
==== ファイルとディレクトリの一覧の取得 ====
指定したディレクトリに含まれるファイルとディレクトリの一覧を取得する場合、<code>os</code>モジュールの<code>listdir</code>関数を使用する。<br>
<br>
引数にディレクトリを指定する時、そのディレクトリに含まれるファイル名とディレクトリ名が格納されたリストを返す。<br>
ファイル名とディレクトリ名は、区別せずにリストに格納される。<br>
<syntaxhighlight lang="python">
os.listdir(path='.')
</syntaxhighlight>
<br>
<syntaxhighlight lang="python">
import os
path = './test/'
filelist = os.listdir(path)
print(filelist)
# 出力
['address.txt', 'doc', 'img', 'name.txt']
</syntaxhighlight>
<br>
なお、引数に指定したディレクトリ名と取得したファイル名およびディレクトリ名を結合することによりパスを取得できるが、<br>
<code>os.path</code>モジュールの<code>join</code>関数を使用した方が簡単である。<br>
複数のパスが格納されているリストを第2引数に指定することにより、ディレクトリの区切り文字(<code>/</code>または<code>\</code>)が自動的に付加されて結合した結果を返す。<br>
<syntaxhighlight lang="python">
os.path.join(path, *paths)
</syntaxhighlight>
<br>
<syntaxhighlight lang="python">
import os
path = './test/'
filelist = os.listdir(path)
for f in filelist:
    print(f)
    print(os.path.join(path, f))
</syntaxhighlight>
<br>
==== ファイルとディレクトリの判別 ====
指定したパスにおいて、ファイルまたはディレクトリを判別する場合、<code>os.path</code>モジュールの<code>isfile</code>関数および<code>isdir</code>関数を使用する。<br>
<br>
<code>isfile</code>関数は、引数に指定したパスが存在、かつ、ファイルの場合は<code>True</code>を返す。<br>
<syntaxhighlight lang="python">
os.path.isfile(path)
</syntaxhighlight>
<br>
<code>isdir</code>関数は、引数に指定したパスが存在、かつ、ディレクトリの場合は<code>True</code>を返す。<br>
<syntaxhighlight lang="python">
os.path.isdir(path)
</syntaxhighlight>
<br>
<code>isfile</code>関数と<code>isdir</code>関数は、ファイルおよびディレクトリが存在しない場合は<code>False</code>を返す。<br>
<syntaxhighlight lang="python">
import os
path = './test/'
print(os.path.isfile(path))
print(os.path.isdir(path))
path = './test/address.txt'
print(os.path.isfile(path))
print(os.path.isdir(path))
# 出力
False
True
True
False
</syntaxhighlight>
<br>
以下の例では、./testディレクトリに含まれるファイルとディレクトリの一覧を取得して、ファイルの場合は[F] + ファイル名、ディレクトリの場合は[D] + ディレクトリ名を出力している。<br>
<syntaxhighlight lang="python">
import os
path = './test/'
filelist = os.listdir(path)
for f in filelist:
    if os.path.isfile(os.path.join(path, f)):
      print('[F]:' + f)
    else:
      print('[D]:' + f)
</syntaxhighlight>
<br>
==== ファイルおよびディレクトリに関する情報も併せて取得する ====
指定したディレクトリに含まれるファイルとディレクトリの一覧を取得する別の方法として、<code>os</code>モジュールの<code>scandir</code>関数を使用する。<br>
<br>
<code>os</code>モジュールの<code>scandir</code>関数は、引数にパスを指定する時、そのパスに含まれるファイルまたはディレクトリに関する情報を持つ<code>os.DirEntry</code>オブジェクトのイテレータを返す。<br>
この情報は、ファイル名やディレクトリ名、ファイルまたはディレクトリの判別、ファイルのサイズ、タイムスタンプ等の情報を持つ。<br>
<br>
<code>os.DirEntry</code>クラスの<code>name</code>属性を参照することによりファイル名またはディレクトリ名を取得することができ、<code>path</code>属性を参照することによりパスを取得することができる。<br>
<syntaxhighlight lang="python">
os.scandir(path = '.')
</syntaxhighlight>
<br>
以下の例では、イテレータから要素を1つ取り出してファイル名とパスをそれぞれ出力している。<br>
<syntaxhighlight lang="python">
import os
path = './test/'
itr = os.scandir(path)
f = next(itr)
print(f.name)
print(f.path)
# 出力
address.txt
./test/address.txt
</syntaxhighlight>
<br>
また、<code>os.DirEntry</code>クラスの<code>is_file</code>メソッド、および、<code>is_dir</code>メソッドを使用することにより、ファイルおよびディレクトリを判別することができる。<br>
<br>
<code>os.DirEntry</code>クラスの<code>is_file</code>メソッドは、ファイルが存在、かつ、ファイルまたはファイルへのシンボリックリンクである場合に<code>True</code>を返す。<br>
<syntaxhighlight lang="python">
is_file(*, follow_symlinks = True)
</syntaxhighlight>
<br>
<code>os.DirEntry</code>クラスの<code>is_file</code>メソッドは、ディレクトリが存在、かつ、ディレクトリまたはディレクトリへのシンボリックリンクである場合に<code>True</code>を返す。<br>
<syntaxhighlight lang="python">
is_dir(*, follow_symlinks = True)
</syntaxhighlight>
<br>
以下の例では、イテレータから要素を1つ取り出して、ファイル名またはディレクトリ名を出力、および、ファイルまたはディレクトリの判別をしている。<br>
<syntaxhighlight lang="python">
import os
path = './test/'
itr = os.scandir(path)
f = next(itr)
print(f.name)
print(f.is_file())
print(f.is_dir())
# 出力
address.txt
True
False
</syntaxhighlight>
<br>
以下の例では、./testディレクトリに含まれるファイルとディレクトリの一覧を取得して、<br>
ファイルの場合は[F] + ファイル名 + パス、ディレクトリの場合は[D] + ディレクトリ名 + パスを出力している。<br>
<syntaxhighlight lang="python">
import os
path = './test/'
for i in os.scandir(path):
    if i.is_file():
      print('[F]:' + i.name + ' ' + i.path)
    else:
      print('[D]:' + i.name + ' ' + i.path)
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>

案内メニュー