Python3-os-listdir

提供:Dev Guides
移動先:案内検索

Python 3-os.listdir()メソッド

説明

メソッド* listdir()*は、pathで指定されたディレクトリ内のエントリの名前を含むリストを返します。 リストは任意の順序です。 特別なエントリ「。」は含まれません。および「..」がディレクトリに存在する場合でも。

pathはstr型またはバイト型のいずれかです。 pathがバイト型の場合、返されるファイル名もバイト型になります。他のすべての状況では、タイプはstrになります。

構文

以下は* listdir()*メソッドの構文です-

os.listdir(path)

パラメーター

*path* -これはディレクトリであり、調査する必要があります。

戻り値

このメソッドは、pathで指定されたディレクトリ内のエントリの名前を含むリストを返します。

次の例は、listdir()メソッドの使用法を示しています。

#!/usr/bin/python3
import os, sys

# Open a file
path = "d:\\tmp\\"
dirs = os.listdir( path )

# This would print all the files and directories
for file in dirs:
   print (file)

結果

上記のプログラムを実行すると、次の結果が生成されます-

Applicationdocs.docx
test.java
book.zip
foo.txt
Java Multiple Inheritance
Java Multiple Inheritance_files
java.ppt
ParallelPortViewer