9cubed
ブログ | Tailwind | Vite | Python | Node.js | Linux | PowerShell | その他 | 将棋ウォーズ | 歌の練習
< 前の記事

Python - パス・ファイル名の操作

次の記事 >

Python - 暗号化コマンドの作成

Python

Python - ディレクトリ・ファイル一覧の取得

公開日:2019-12-06
更新日:2019-12-06

1. 概要

ディレクトリやファイルの一覧の取得を行います。

2. listdir

ディレクトリ名やファイル名の一覧を取得できます。
絶対パスを取得したい場合は、os.path.abspath() で変換してください。
また、リスト内包表記により、条件を指定して絞り込むこともできます。
import os

# ディレクトリとファイルの一覧の取得(名前のみ)
print( os.listdir('d:/python/test'))
# ['.vscode', 'dir1', 'dir2', 'test.py']

# ディレクトリ一覧の取得(ディレクトリ名のみ)
dir_list = [
    path for path in os.listdir('d:/python/test')
         if os.path.isdir(path)
]
print(dir_list) # ['.vscode', 'dir1', 'dir2']

# ディレクトリ一覧の取得(絶対パス)
dir_list = [
    os.path.abspath(path) for path in os.listdir('d:/python/test')
         if os.path.isdir(path)
]
print(dir_list)
# ['D:\\python\\test\\.vscode', 'D:\\python\\test\\dir1', 'D:\\python\\test\\dir2']

# ファイル一覧の取得
file_list = [
    path for path in os.listdir('d:/python/test')
         if os.path.isfile(path)
]
print(file_list) # ['test.py']

3. glob

glob は正規表現を使うことができます。
import glob

print('---------- 1')

# 指定したディレクトリ配下のディレクトリとファイルの一覧
for path in glob.glob('d:/python/test/**/*', recursive = True):
    print(path)
    # d:/python/test\dir1
    # d:/python/test\dir2
    # d:/python/test\test.py
    # d:/python/test\dir1\test1.txt    

print('---------- 2')

# 指定したディレクトリ配下のファイルの一覧
for path in glob.glob('d:/python/test/**/*.*' ,recursive = True):
    print(path)
    # d:/python/test\test.py
    # d:/python/test\dir1\test1.txt

print('---------- 3')

# 指定したディレクトリ配下のディレクトリの一覧
for path in glob.glob('d:/python/test/**/' ,recursive = True):
    print(path)
    # d:/python/test\
    # d:/python/test\dir1\
    # d:/python/test\dir2\

print('---------- 4')

# 指定したディレクトリ直下のディレクトリとファイルの一覧
for path in glob.glob('d:/python/test/*'):
    print(path)
    # d:/python/test\dir1
    # d:/python/test\dir2
    # d:/python/test\test.py

print('---------- 5')

# 指定したディレクトリ直下のファイルの一覧
for path in glob.glob('d:/python/test/*.*'):
    print(path)
    # d:/python/test\test.py

4. pathlib.Path.iterdir

listdir と glob は一括で取得しますが、
pathlib.Path.iterdir は1つずつ取得します。
一覧取得の途中で処理を抜ける場合や、途中経過の出力、キャンセル処理などを行う場合は、
Path.iterdir の使用を検討してみてください。

from pathlib import Path

for path in Path('d:/python/test').iterdir():
    print(path)
    # d:\python\test\.vscode
    # d:\python\test\dir1
    # d:\python\test\dir2
    # d:\python\test\test.py

< 前の記事

Python - パス・ファイル名の操作

次の記事 >

Python - 暗号化コマンドの作成

YouTube X

新着一覧

  • SCSS のインストールVite
  • Tailwind CSS のプロジェクトの作成Tailwind
  • TypeScriptのプロジェクトの作成Vite
  • Flask のインストールと動作確認Python
  • 簡易Webサーバーの作成Python
  • pipeline で文章の生成Python
  • pipeline で文章の要約Python
  • 音声から文字起こしPython
  • Node.js のインストールNode.js
  • .ps1(PowerShellスクリプト)を実行可能にするPowerShell

アーカイブ

  • 2025/12
  • 2025/11
  • 2025/10
  • 2025/09
  • 2025/08

以前のカテゴリー一覧

  • CakePHP3
  • CentOS7
  • HTML・CSS・JavaScript
  • Haskell
  • JavaScript
  • Kotlin
  • Laravel5
  • PHP
  • Python
  • Ruby
  • RubyOnRails5
  • TypeScript
  • Vue.js
  • Webサーバ講座
  • Webプログラミング講座
  • jQuery
  • linux
  • パソコン講座
  • ブログ
  • プログラミング講座
  • メモ帳作成講座
  • 数学

Copyright © 9cubed. All Rights Reserved.

プライバシーポリシー 利用規約
▲