Last active
December 19, 2024 15:17
-
-
Save Pymmdrza/6c19674bb67aa6d8ebb122e436fc0ab6 to your computer and use it in GitHub Desktop.
PyMermid Auto create path file's and folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def create_project_structure_from_file(file_path, base_directory="PyMermid"): | |
""" | |
Create a file and folder structure based on the tree-like format in the input text file. | |
Args: | |
- file_path (str): The path to the text file containing the directory structure. | |
- base_directory (str): The base directory where the project structure will be created. | |
""" | |
# Keep track of the current directory stack | |
directory_stack = [] | |
with open(file_path, 'r', encoding='utf-8') as file: | |
lines = file.readlines() | |
for line in lines: | |
# Remove leading and trailing whitespace | |
line = line.rstrip() | |
# Skip empty lines | |
if not line: | |
continue | |
# Calculate the depth of the current line based on "│", "├──", or "└──" | |
depth = line.count('│') + line.count('├──') + line.count('└──') | |
# Extract the name of the directory or file | |
name = line.split('├──')[-1].split('└──')[-1].strip() | |
# Determine if the line represents a directory or a file | |
is_directory = not '.' in name | |
# Trim the stack to match the current depth | |
if depth < len(directory_stack): | |
directory_stack = directory_stack[:depth] | |
# Add the current directory or file to the directory stack | |
if is_directory: | |
# This is a directory, so update the stack | |
current_path = os.path.join(base_directory, *directory_stack, name) | |
if not os.path.exists(current_path): | |
os.makedirs(current_path) | |
directory_stack.append(name) | |
else: | |
# This is a file, so create it | |
current_path = os.path.join(base_directory, *directory_stack, name) | |
parent_dir = os.path.dirname(current_path) | |
if not os.path.exists(parent_dir): | |
os.makedirs(parent_dir) | |
# Create an empty file if it doesn't exist | |
with open(current_path, 'w', encoding='utf-8') as f: | |
f.write('') | |
print(f"Successfully Created File: {current_path}") | |
if __name__ == "__main__": | |
# The path to the text file containing the project structure | |
input_file_path = input("Enter the path to the text file: ") | |
# The base directory where the project structure will be created | |
base_directory = 'PyMermid' | |
create_project_structure_from_file(input_file_path, base_directory) | |
print(f"Project structure successfully created in '{base_directory}' directory.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PythonProject/ | |
├── app/ | |
│ ├── init.py | |
│ ├── config/ | |
│ │ └── config.py | |
│ ├── routes.py | |
│ ├── templates/ | |
│ │ ├── Base.html | |
│ │ ├── index.html | |
│ │ ├── User/ | |
│ │ │ ├── Tickets.html | |
│ │ │ ├── Login.html | |
│ │ │ ├── SignUp.html | |
│ │ │ └── Home.html | |
│ │ └── Admin/ | |
│ │ ├── home.html | |
│ │ └── tickets.html | |
│ ├── controllers/ | |
│ │ ├── auth_controller.py | |
│ │ └── ticket_controller.py | |
│ ├── models/ | |
│ │ ├── user_model.py | |
│ │ └── ticket_model.py | |
│ ├── services/ | |
│ │ ├── notification_service.py | |
│ │ └── email_service.py | |
│ └── static/ | |
│ ├── css/ | |
│ │ └── styles.css | |
│ └── js/ | |
│ └── scripts.js | |
├── docker/ | |
│ └── Dockerfile | |
├── docker-compose.yml | |
├── setup.sh | |
├── run_clean.sh | |
├── run_demo.sh | |
└── requirements.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment