diff --git a/comprehensions.html b/comprehensions.html index 8d6ba60..747e518 100644 --- a/comprehensions.html +++ b/comprehensions.html @@ -293,14 +293,16 @@ body{counter-reset:h1 3}
>>> import os, glob, humansize
->>> humansize_dict = {os.path.splitext(f)[0]:humansize.approximate_size(os.stat(f).st_size) \
-... for f in glob.glob('*') if os.stat(f).st_size > 6000} ①
->>> list(humansize_dict.keys()) ②
+>>> metadata_dict = {f:os.stat(f) for f in glob.glob('*')} ①
+>>> humansize_dict = {os.path.splitext(f)[0]:humansize.approximate_size(meta.st_size) \
+... for f, meta in metadata_dict.items() if meta.st_size > 6000} ②
+>>> list(humansize_dict.keys()) ③
['romantest9', 'romantest8', 'romantest7', 'romantest6', 'romantest10', 'pluraltest6']
->>> humansize_dict['romantest9'] ③
+>>> humansize_dict['romantest9'] ④
'6.5 KiB'
glob.glob('*')), filters that list to include only those files larger than 6000 bytes (if os.stat(f).st_size > 6000), and uses that filtered list to construct a dictionary whose keys are the filename minus the extension (os.path.splitext(f)[0]) and whose values are the approximate size of each file (humansize.approximate_size(os.stat(f).st_size)).
+glob.glob('*')), gets the file metadata for each file (os.stat(f)), and constructs a dictionary whose keys are filenames and whose values are the metadata for each file.
+6000 bytes (if meta.st_size > 6000), and uses that filtered list to construct a dictionary whose keys are the filename minus the extension (os.path.splitext(f)[0]) and whose values are the approximate size of each file (humansize.approximate_size(meta.st_size)).
approximate_size() function.