Package: pyosplus
pyosplusThis Python package provides several useful functions based onos/os.path:count_in_dirext_filesfind_files_dirsinc_namewrite_dir_treeThese functions are really primitive but they happen to be used quite often by some people.Please feel free to report any bugs.RequirementsPython 3.10 or higher.Installationpip install pyosplus(seehttps://pypi.org/project/pyosplus/)Functionscount_in_dircount_in_dir(directory:str,scan_subdirs:bool,ignored_exts:list[str]|str=[],)->tuple[int,int,dict]Argumentsdirectory: strDirectory to scan.scan_subdirs: boolTo scan subdirectories (True) or not (False).ignored_exts: list[str] | str = []Extension(s) of files to be ignored. Each extension should start with.(dot). Extension checks are always case-insensitive (e.g.,".jpg"is the same as".JPG").Returnstuple[int, int, dict]Number of directories, number of files, and dictionary with numbers of files with each found extension. Files with extensionsignored_extsare not counted.Minimal Examplefrompyosplusimportcount_in_dirdirectory="/path/to/dir"scan_subdirs=Truenum_dirs,num_files,ext_count=count_in_dir(directory,scan_subdirs)ext_filesext_files(directory:str,extensions:str|list[str],scan_subdirs:bool,)->list[str]Argumentsdirectory: strDirectory to scan.extensions: str | list[str]Extension(s) of files to search. Each extension should start with.(dot). Extension checks are always case-insensitive (e.g.,".jpg"is the same as".JPG").scan_subdirs: boolTo scan subdirectories (True) or not (False).Returnslist[str]Sorted list of paths to files withextensions.Minimal Examplefrompyosplusimportext_filesdirectory="/path/to/dir"extensions=[".jpg",".jpeg"]paths=ext_files(directory,extensions)find_files_dirsfind_files_dirs(what:str|list[str],where:str|list[str],where_not:str|list[str]=[],name_mode:str="part",type_mode:str="both",ignore_case:bool=True,)->Generator[str,None,None]Argumentswhat: str | list[str]String(s) to search in names of files/directories.where: str | list[str]Path(s) to the directories where to search.where_not: str | list[str] = []Path(s) to the directories to exclude from search. They should be subdirectories ofwhere.name_mode: str = "part""full"—whatis the full name(s) of files/directories,"part"—whatis the name part(s) of files/directories,"end"—whatis the name end(s) of files/directories.type_mode: str = "both""both"— search among files and directories,"files"— search among files only,"dirs"— search among directories only.ignore_case: bool = TrueIgnore the case or not:True—"ABC"is equal to"abc",False—"ABC"is not equal to"abc".It works forwhatonly. Paths inwhereandwhere_notshould have the same case as in the system.ReturnsGenerator[str, None, None]Generator of paths to the found files/directories.Minimal Examplefrompyosplusimportfind_files_dirswhat="thesis"where="/path/to/dir"forpathinfind_files_dirs(what,where):print(path)inc_nameinc_name(path:str,width:int=2,sep_1:str=".",sep_2:str="",start:int=2,use_ext:bool=True,)->strArgumentspath: strPath to file/directory to be incremented.width: int = 2Argument ofzfill()to fill the counter with leading zeros.sep_1: str = "."Separator before the counter.sep_2: str = ""Separator after the counter.start: int = 2The counter first value.use_ext: bool = TrueIfTrue, the counter will be put before the file extension.IfFalse, the counter will be put at the end of string.The latter is for the directories with dot(s) in the names.ReturnsstrIncrementedpathifpathalready exists orpathitself if it does not exist.Minimal Examplefrompyosplusimportinc_namepath="/path/to/file.txt"inc=inc_name(path)It returns:"/path/to/file.txt"if this file does not exist;"/path/to/file.02.txt"if"/path/to/file.txt"exists and"/path/to/file.02.txt"does not exist;etc.write_dir_treewrite_dir_tree(directories:str|list[str],html_file:str,print_exts:bool=True,num_spaces:int=4,shrunk_dirs:str|list[str]=[],shrunk_depth:int=-1,shrunk_text:str=" &lt;...&gt;",ignored_exts:str|list[str]=[],ignored_paths:str|list[str]=[],print_root:bool=True,captions:list[str]=[],print_hr:bool=True,)Argumentsdirectories: str | list[str]Directory/directories to scan.html_file: strPath to a new HTML file for output. If it exists, it will be overwritten.print_exts: bool = TruePrint file extensions (True) or not (False).num_spaces: int = 4Number of spaces for indentation.shrunk_dirs: str | list[str] = []Path(s) to the specific directories to be shrunk (i.e., collapsed) in HTML.shrunk_depth: int = -1The depth (i.e., hierarchy level) from which all the directories should be shrunk (collapsed) in HTML. The depth ofdirectoriesequals 0.shrunk_depth = -1means that no directories should be shrunk except those inshrunk_dirs(if any).shrunk_text: str = " &lt;...&gt;"The text to be put next to a shrunk directory name.ignored_exts: str | list[str] = []Extensions of files to be ignored in HTML. Such files will not be visible in HTML at all. Each extension should start with.(dot). Extension checks are always case-insensitive (e.g., ".jpg" is the same as ".JPG").ignored_paths: str | list[str] = []Paths to the directories/files to be ignored. Unlike the shrunk directories,ignored_pathswill not be visible in HTML at all.print_root: bool = TruePrint a root directory (True) or not (False).captions: list[str] = []List of captions to appear before the tree for each directory fromdirectories. Ifcaptionsare not empty, the lengths ofcaptionsanddirectoriesshould be equal so there exists a caption for each directory (in the same order as in these lists).print_hr: bool = TruePrint a horizontal line (True) or not (False).ReturnsNone. Writes a directory tree structure tohtml_file.Minimal Examplefrompyosplusimportwrite_dir_treedirectories=["/path/to/dir_1","/path/to/dir_2"]html_file="tree.html"write_dir_tree(directories,html_file)ChangelogVersion 1.3.0 (2023-12-23)argumentcaptionsadded towrite_dir_tree.Version 1.2.0 (2023-11-11):functionwrite_html_dir_treeremoved.Version 1.1.0 (2023-11-07):functionwrite_dir_treeadded,functionwrite_html_dir_treedeprecated,the default values ofscan_subdirsin functionscount_in_dirandext_filesremoved.Version 1.0.0 (2023-11-05): initial releasepyosplusVersion 1.3.0 (2023-12-23)Copyright (c) 2023 Evgenii ShirokovMIT License