1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ディレクトリ配下のファイルアクセス

Posted at

ディレクトリ配下に存在するファイルへループでアクセスする方法memo

pythonであるディレクトリ内の全ファイルに対して処理をしたいが,
毎回忘れるためメモ.

fild_all_files.py
def fild_all_files(directory):
    for root, dirs, files in os.walk(directory):
        yield root
        for file in files:
            yield os.path.join(root, file)

引数に対象ディレクトリのパスを渡してループするだけでファイルの中身が返ってくる.

使用例

main.py
for filename in fild_all_files(file_dir):
    print filename

以上

1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?