Lsdir is a Python class that provides a set of methods to interact with the file system. It allows you to scan directories, count the number of files and directories, print the directory tree, and find items by their ID. It also provides the ability to read the content of text files.
-
Scan directories: The
scan_dirmethod scans a given directory and returns a list of its children (both files and directories). Each child is represented as a dictionary with the following keys:id,name,type,path, andchildren(for directories) orfiletypeandcontent(for text files). -
Count levels: The
count_levelsmethod counts the number of levels in a given directory tree. -
Print directory tree: The
print_treemethod prints the directory tree in a human-readable format. -
Find items by ID: The
find_item_by_idmethod finds an item (file or directory) by its ID. -
Count files and directories: The
count_files,count_dirs, andcount_allmethods count the number of files, directories, and total items, respectively, in a given directory tree.
First, import the necessary modules and the Lsdir class:
import os
import mimetypes
from lsdir import LsdirThen, create an instance of the Lsdir class:
files = Lsdir()Now, you can use the methods of the Lsdir class. For example, to scan the current working directory and print its tree:
data = files.scan_cwd()
files.print_tree(data)To find an item by its ID:
item = files.find_item_by_id(101, data)
print(item)To count the number of files, directories, and total items:
num_files = files.count_files(data)
num_dirs = files.count_dirs(data)
total = files.count_all(data)
print(f"Files: {num_files}, Directories: {num_dirs}, Total: {total}")- Python 3.6 or higher
osandmimetypesmodules (included in the standard Python library)
This project is licensed under the Gnu v3 License.