Recipes¶
You will need to import fluentfs first:
import fluentfs as fs
Example recipes¶
Number of files and directories in a directory¶
Get the number of “file-like” objects (i.e. files & directories) in a directory (including subdirectories):
fs.Dir(dir).file_likes.len()
Get the number of files in a directory (including subdirectories):
fs.Dir(dir).files.len()
Get the number of directories in a directory (including subdirectories):
fs.Dir(dir).dirs.len()
Biggest files in a directory¶
Get a table with the n biggest files in a directory (including subdirectories):
(
# e.g. dir_path = ".", n = 20
fs.Dir(dir_path) # 1.
.files # 2.
.top_n(n) # 3.
.table( # 4.
["File path", "Size"],
lambda f: (f.relative_path, f.size)
)
)
We get a
Dirobject whose path isdir_path.We get a
FileIteratorfor the files of the directory atdir_path.The
FileIteratoris aFunctionalIterator, so it has thetop_nmethod to get thenbiggest items in the iterator. Since files are compared by size influentfs, we can use it to get thenbiggest files. Thetop_nfunction returns anotherFunctionalIterator.We can obtain a
Tablefrom anyFunctionalIteratorby calling thetablemethod. This method takes a list of column names and a function which maps every element of theFunctionalIteratorto a row. Therefore we get a table where the column “File path” will be populated with the relative paths of the files (f.relpath) and the column “Size” will be populated with the file sizes (f.size).
Counting lines and empty lines¶
Get a sorted table with all Python files in a directory together with their total lines, source lines and blank lines.:
(
# e.g. dir_path = "."
fs.Dir(dir_path) # 1.
.files # 2.
.filter_extension("py") # 3.
.text_file_iterator() # 4.
.sort_desc(lambda f: f.line_count) # 5.
.table( # 6.
["File", "Total lines", "Source lines", "Blank lines"],
lambda f: (f.relative_path, f.line_count, f.non_empty_line_count, f.empty_line_count)
)
)
We get a
Dirobject whose path isdir_path.We get a
FileIteratorfor the files of the directory atdir_path.We filter the
FileIteratorby the “py” extension, which returns anotherFileIterator.Since we are about to perform text file operations, we need to obtain a
TextFileIteratorfrom theFileIteratorusing thetext_file_iteratorfunction. Since we filtered by the “py” extension beforehand, we can be relatively sure that we only have text files. Of course theoretically nothing would prevent us from having a binary file with the “py” extension in our directory. In that casetext_file_iteratorwould still succeed, but any further operation would fail when we try to decode those binary files.We use the
sort_descfunction together with a lambda that specifies the sort key (similar to how regular Pythonsortworks) to sort the files by their total line counts.We can obtain a
Tablefrom anyFunctionalIteratorby calling thetablemethod. This method takes a list of column names and a function which maps every element of theFunctionalIteratorto a row. Therefore we get a table where the columns will be populated with the relative file path and the number of lines, non-blank lines and blank lines.