1
3

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 1 year has passed since last update.

Pythonでファイル名一覧を取得

Last updated at Posted at 2022-04-14

任意のフォルダ内のファイル名とフォルダ名を全取得

sudoフォルダ内のファイル名とフォルダ名を取得したいときは,

import os
file_names = os.listdir('sudo/')

でlistで返ってくる.

任意のフォルダ内の任意の拡張子のついたファイル名を全取得

フォルダを含めたくない時,あるいは拡張子を指定したい場合は,

import glob
file_paths = glob.glob('sudo/*.wav')

で欲しい拡張子のついているもののみをlistで取得できる.こちらはファイル名だけでなく相対パスも含まれることに注意.

相対パスが要らないのであれば,

file_names = []
for file in file_paths:
    tmp = os.path.split(file)[1]
    file_names.append(tmp)

で除去できる.

1
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?