Heim > Fragen und Antworten > Hauptteil
Ich habe einen Ordner/tmp
, woher weiß ich, wie viele Dateien sich darin befinden
import os
DIR = '/tmp'
result = [name for name in os.listdir(DIR)]
Das Ergebnis sind alle Dateien und Ordner unter DIR. So ermitteln Sie die Anzahl der Dateien
某草草2017-06-28 09:27:27
参考文章:Python文件操作相关问题
>>> import os
>>> DIR = '/tmp'
>>> print len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
如统计文件夹数量,用os.path.isdir(path)
做判断语句。