Flask는 생성 시 폴더구조가 생성되지 않아 매우 유연하다.
그래서 한편으로는 폴더구조에 대해 고민이 많았는데, 어떤 구조가 좋을지는 프로젝트별로 다르다.
1. 일반적인 폴더구조
- 역할별로 구조화
- 코드가 역할별로 분리되어 있어 간단하고 직관적이다.
- 작은 프로젝트에 적합하다.
- 프로젝트가 커지면 디렉토리가 혼란스러워질 수 있고 관리가 어려워질 수 있다.
- 협업 시 비효율적이다.
project/
├── routes/
│ ├── main.py
│ ├── auth.py
├── services/
│ ├── db_service.py
│ ├── auth_service.py
├── static/
│ ├── css/
│ ├── js/
│ └── img/
├── templates/
│ ├── index.html
│ └── auth/
│ └── login.html
├── app.py
├── config.py
└── requirements.txt
2. 확장된 폴더 구조
- 기능별로 구조화
- 각 모듈이 독립적으로 설계되어 특정 기능 수정, 추가 시 해당 디렉토리만 수정하면 된다
- 팀 프로젝트에 유리 : 기능별로 별도의 디렉토리에서 작업할 수 있어 충돌이 적을 수 있다.
- 디렉토리 세분화시 관리가 복잡하고 불편할 수 있다.
project/
├── app/
│ ├── __init__.py
│ ├── main/
│ │ ├── __init__.py
│ │ ├── routes.py
│ │ └── templates/
│ │ └── main/
│ │ └── index.html
│ ├── auth/
│ │ ├── __init__.py
│ │ ├── routes.py
│ │ └── templates/
│ │ └── auth/
│ │ └── login.html
│ └── static/
│ └── services/
├── config.py
├── run.py
└── requirements.txt
3. 일반적인 폴더구조 한번에 생성하기
- 폴더를 하나 하나 만들려니까 너무 귀찮아서 코드 실행 시 한 번에 만들어지도록 작성했다.
import os
def create_project_structure(base_dir):
"""백엔드 폴더 구조 생성"""
structure = {
"app": {
"__init__.py": None,
"routes": {
"home.py": None,
"user.py": None,
"admin.py": None,
},
"services": {
"auth_service.py": None,
"db_service.py": None,
},
"models": {
"user.py": None,
"post.py": None,
},
"static": {},
"templates": {
"index.html": None,
},
"config.py": None,
"utils.py": None,
},
"run.py": None,
"requirements.txt": None,
".env": None,
".gitignore": None,
}
def create_structure(base, struct):
for name, content in struct.items():
path = os.path.join(base, name)
if isinstance(content, dict):
os.makedirs(path, exist_ok=True)
create_structure(path, content)
else:
if content is not None:
with open(path, 'w', encoding='utf-8') as f:
f.write(content)
else:
open(path, 'a').close()
create_structure(base_dir, structure)
# 실행
base_directory = "be" # 루트 디렉토리
create_project_structure(base_directory)
print(f"Project structure created at {base_directory}")
- 위 코드 실행시 be/ 디렉토리에 각종 폴더와 파일들이 내용 없는 채로 생성된다.
- generate_structure.py 파일이 한 번에 필요한 폴더와 파일을 생성하는 파일이다.

'Programming' 카테고리의 다른 글
| Jquery, 이제는 안 써도 될까? (1) | 2025.02.05 |
|---|---|
| [python] requirements.txt로 설치한 패키지 관리하기 (0) | 2024.12.25 |
| [자바] 문자열에서 특정 문자 출력하기 (0) | 2023.07.02 |
| [자바]sc.nextLine(); 안써서 입력 안 되는 오류 (0) | 2023.06.20 |