0
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 5 years have passed since last update.

Python os関連 ファイル,サブディレクトリ操作

Posted at

Python ディレクトリ名取得

わかりやすいものがなかったので、だいぶ内容はかぶっていると思いますが一応備忘録的に。

os.walkで手に入るのがgeneratorオブジェクトなので扱いやすいようにリストに変換

dir = list(os.walk("path"))

なぜだか、os.walkで表示させたがりますが、私がしたいのはパスの取得なので構造を知りたかったのです。
手に入るのは

('rootディレクトリ',[ディレクトリ(lsした時に表示されるディレクトリ)],[ファイル(同様にファイル)])

というタプル構造が再帰的に含まれているリスト構造です。
dir[0] には引数のpathのタプルが
dir[1] にはディレクトリ1番目のタプル構造が
、、、
という感じです。

ディレクトリやファイル名取得する際、他にもっといい方法があるのかもしれませんがぱっと探しても割と出てこなかったのでとりあえずはこれで行こうかと。

もっと楽な方法があるよという方、コメントいただけると嬉しいです。

0
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
0
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?