LoginSignup
4
5

More than 5 years have passed since last update.

Python > 複数のディレクトリにあるファイルの一覧を取得する > glob使用 | modification timeでのソート

Last updated at Posted at 2017-07-21
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.1.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

bash > 複数ディレクトリ内のファイルのTFRerocds化 (個別処理)
http://qiita.com/7of9/items/6f6ffdb7dbcebdc8d923
にて作成した複数ディレクトリのTFRecordsをまとめる処理を実装中。

まずはファイルリストの取得。

globというのを使うようだ。
http://www.yukun.info/blog/2008/08/python-directory-listdir-glob.html
情報感謝です。

combine_TFRecords_170722.py
import glob

"""
v0.1 Jul. 22, 2017
    - get file list
"""

# codingrule:PEP8

res = glob.glob("../run*/IntField-Y_170709.tfrecords")
for elem in res:
    print(elem)

$ python3 combine_TFRecords_170722.py 
../run366_sphere_g26_m1.45/IntField-Y_170709.tfrecords
../run375_sphere_g26_m1.5/IntField-Y_170709.tfrecords
../run354_sphere_g26_m1.33/IntField-Y_170709.tfrecords
../run362_sphere_g26_m1.4/IntField-Y_170709.tfrecords
../run364_sphere_g26_m1.4/IntField-Y_170709.tfrecords
../run368_sphere_g26_m1.45/IntField-Y_170709.tfrecords
../run370_sphere_g26_m1.45/IntField-Y_170709.tfrecords
../run376_sphere_g26_m1.5/IntField-Y_170709.tfrecords
../run367_sphere_g26_m1.45/IntField-Y_170709.tfrecords
../run356_sphere_g26_m1.33/IntField-Y_170709.tfrecords
../run372_sphere_g26_m1.5/IntField-Y_170709.tfrecords
../run355_sphere_g26_m1.33/IntField-Y_170709.tfrecords
../run360_sphere_g26_m1.4/IntField-Y_170709.tfrecords
../run373_sphere_g26_m1.5/IntField-Y_170709.tfrecords
../run365_sphere_g26_m1.45/IntField-Y_170709.tfrecords
../run369_sphere_g26_m1.45/IntField-Y_170709.tfrecords
../run353_sphere_g26_m1.33/IntField-Y_170709.tfrecords
../run357_sphere_g26_m1.33/IntField-Y_170709.tfrecords
../run371_sphere_g26_m1.5/IntField-Y_170709.tfrecords
../run359_sphere_g26_m1.4/IntField-Y_170709.tfrecords
../run358_sphere_g26_m1.33/IntField-Y_170709.tfrecords
../run374_sphere_g26_m1.5/IntField-Y_170709.tfrecords
../run363_sphere_g26_m1.4/IntField-Y_170709.tfrecords
../run361_sphere_g26_m1.4/IntField-Y_170709.tfrecords

modification timeでのソート

(追記 2017/10/03)

参考: https://stackoverflow.com/questions/6773584/how-is-pythons-glob-glob-ordered

glob.glob()実行での並びはls -Uと同じになるとのこと。

modification timeでのソートをするにはsort()を使うとのこと。

import os
sorted(glob.glob('*.png'), key=os.path.getmtime)
4
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
4
5