括弧のない簡便さ、行末のセミコロンなし、インデント必須ってのははまりますね。
ライブラリの豊富さってのはプログラミング言語のすべてみたいなもんですが、こいつばかりは人気がモノを言います。
もっとも好まれる言語で選ばれる Python ならではの強みですね。
その Python でディレクトリ中のファイルをリストアップするスクリプトを書いてみました。
path でディレクトリを、type でソートのキーを指定します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
# Sorts files by date, name, size, type | |
def listdir_sorted(path,type): | |
lst=lambda f:f[1] | |
if type=="date": | |
lst = lambda f: os.stat(os.path.join(path, f)).st_mtime | |
elif type=="name": | |
lst = lambda f: f[1] | |
elif type=="size": | |
lst = lambda f: os.stat(os.path.join(path, f)).st_size | |
elif type=="type": | |
lst = lambda f: os.path.splitext(f)[1] | |
return list(sorted(os.listdir(path), key=lst, reverse=False)) |