Create directory list in markdown format
import os
path="."
generate_directory_structure_markdown(path="/home/user/")
The format_size function takes a file size in bytes as input and returns a formatted string representing the size in kilobytes (kB), megabytes (MB), or gigabytes (GB) depending on the magnitude of the size.
Example Usage
size = 1024 formatted_size = format_size(size) print(formatted_size)
Code Analysis
Inputs:
size
(integer): The file size in bytes.
Flow:
- Divide the size by 1024 to convert it to kilobytes.
- If the resulting size is less than 1024, return the size formatted as kilobytes with one decimal place (e.g., "1.2 kB").
- If the resulting size is between 1024 and 1024 * 1024 (exclusive), divide it by 1024 to convert it to megabytes and return the size formatted as megabytes with one decimal place (e.g., "1.2 MB").
- If the resulting size is greater than or equal to 1024 * 1024, divide it by 1024 * 1024 to convert it to gigabytes and return the size formatted as gigabytes with one decimal place (e.g., "1.2 GB").
Outputs:
- A formatted string representing the file size in kilobytes (kB), megabytes (MB), or gigabytes (GB).
The repeat_string function takes in two parameters: times and string. It returns a string that is the concatenation of string repeated times number of times.
Example Usage
repeat_string(3, ">")
This will return ">>>" because the string ">" is repeated 3 times.
Code Analysis
Inputs:
times
(integer): The number of times the string should be repeated.string
(string): The string to be repeated.
Flow:
- Check if the value of times is less than 1. If it is, return an empty string.
- If times is greater than or equal to 1, multiply the string by times and return the result.
Outputs:
- A string that is the concatenation of string repeated times number of times.
This code defines a function called generate_directory_structure_markdown that generates a markdown file (directory_structure.md) representing the directory structure of a given path. The function uses the os.walk function to traverse the directory tree and writes the directory structure to the markdown file.
Example Usage
generate_directory_structure_markdown("/path/to/directory")
This code will generate a markdown file named directory_structure.md in the current directory, representing the directory structure of the /path/to/directory.
Code Analysis
Inputs:
path
(optional): A string representing the path of the directory to generate the directory structure for. If not provided, the current directory is used.
Flow:
- Open the directory_structure.md file in write mode.
- Write the header line to the file.
- Traverse the directory tree using os.walk, iterating over each root directory, subdirectories, and files.
- Exclude the venv directory from the subdirectories list.
- Filter out directories starting with a dot (.) or ending with .gitignore or .ignore.
- Filter out files starting with a dot (.) or ending with .gitignore or .ignore.
- Exclude the directory_structure.md file from the files list.
- Calculate the current depth of the directory in the tree.
- Write the directory name and icon to the file, with appropriate indentation based on the depth.
- Iterate over each file in the directory.
- Calculate the depth for the file based on the current depth.
- Get the file path and size.
- Format the file size in kilobytes (kB), megabytes (MB), or gigabytes (GB) based on its size.
- Write the file name, icon, and size to the file, with appropriate indentation based on the depth.
Outputs:
- None: The function writes the directory structure to the
directory_structure.md
file.