{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# U/System/List Folder Contents\n", "\n", "The module `lsr` returns a list, a DataFrame, or a string that describes the content of a given folder.\n", "The returned formats are designed to either present the information or to be used in further processing.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "To prepare an example for using `mtbp3`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import shutil\n", "import random\n", "\n", "\n", "random_number = random.randint(1, 100)\n", "testfolder_name = f\"testfolder_{random_number}\"\n", "\n", "os.mkdir(testfolder_name)\n", "os.mkdir(f\"{testfolder_name}/subfolder2\")\n", "os.mkdir(f\"{testfolder_name}/subfolder1\")\n", "with open(f\"{testfolder_name}/subfolder2/testfile3\", \"w\") as file:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use the LsrTree:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from mtbp3.util.lsr import LsrTree" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To list files in a folder:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lsr = LsrTree(testfolder_name, outfmt=\"tree\")\n", "print(lsr.list_files())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To list files in a folder with counts added after folder names:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lsr = LsrTree(testfolder_name, outfmt=\"tree\", with_counts=True)\n", "print(lsr.list_files())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To list files with file sizes and dates:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "lsr = LsrTree(testfolder_name, outfmt=\"dataframe\")\n", "print(lsr.list_files())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To remove the test folder:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "shutil.rmtree(testfolder_name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other Options\n", "\n", "Window:\n", "\n", "
\n",
                "tree /f > tree.txt\n",
                "
\n", "\n", "Linux/Mac OS:\n", "\n", "
\n",
                "tree\n",
                "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.1" } }, "nbformat": 4, "nbformat_minor": 4 }