This script generates a directory structure, counts files in directories with specified exclusion patterns, and can read content from files with specified names or extensions. Additionally, it supports logging of its actions and saving output to a file.
Python 3.6+
prettytable
module
Install the required module using:
pip install prettytable
The script can be used to generate a directory structure, count files, and read file contents based on extensions. The following arguments are available:
--path
: Path to the directory (required).--exclude
: List of regex patterns to exclude directories/files (optional).--file-names
: List of specific file names or extensions to include content from (optional).--log-file
: File to save log to. If not specified, logs to console. If specified but empty, logs to a default file in the parsed directory (optional).--log-level
: Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) (optional).--output-file
: File to save output to. If specified but empty, saves to a default file in the parsed directory (optional).
1. Generate directory structure and log to console:
python directory_structure.py --path /path/to/directory
2. Generate directory structure with exclusions:
python directory_structure.py --path /path/to/directory --exclude 'pattern1' 'pattern2'
3. Read content from files with specified names or extensions:
python directory_structure.py --path /path/to/directory --file-names file1.txt file2.md .py
4. Save log to a file:
python directory_structure.py --path /path/to/directory --log-file /path/to/logfile.log
5. Save output to a file:
python directory_structure.py --path /path/to/directory --output-file /path/to/output.txt
python directory_structure.py --path /path/to/directory --exclude 'pattern1' 'pattern2' --file-names file1.txt file2.md .py --log-file --output-file
This will:
- Generate the directory structure for
/path/to/directory
. - Exclude files and directories matching
pattern1
andpattern2
. - Include contents of files named file1.txt and file2.md, and files with the .py extension.
- Save logs to a default file in the directory.
- Save output to a default file in the directory.
Directory Structure:
my_project/
├── README.md
├── src/
│ ├── main.py
│ ├── utils.py
│ └── __init__.py
├── tests/
│ ├── test_main.py
│ └── __init__.py
└── requirements.txt
Directory File Count:
+------------+-------------------------------+
| File Count | Directory |
+------------+-------------------------------+
| 3 | /path/to/directory/src |
| 2 | /path/to/directory/tests |
| 1 | /path/to/directory |
+------------+-------------------------------+
Files Content:
========================================
Content of README.md:
========================================
# My Project
This is the README file for My Project.
========================================
Content of requirements.txt:
========================================
prettytable
The script logs its actions, including errors and debug information. By default, logs are printed to the console. If a log file is specified, logs are saved to that file.
python directory_structure.py --path /path/to/directory --log-file /path/to/logfile.log --log-level DEBUG
python directory_structure.py --path /path/to/directory --output-file /path/to/output.txt
If an error occurs, the script will log the error and print an error message. If a log file is specified, more details can be found in the log file.
To execute the script, run:
python directory_structure.py --path /path/to/directory
Replace /path/to/directory
with the actual path of the directory you want to process.