72 lines
2.8 KiB
Python
72 lines
2.8 KiB
Python
import os
|
|
import zipfile
|
|
|
|
# list.txt에서 파일명을 읽어오는 함수
|
|
def read_file_list():
|
|
list_file = os.path.join(os.getcwd(), 'list.txt')
|
|
if not os.path.isfile(list_file):
|
|
raise ValueError(f"'{list_file}'은(는) 파일이 아니거나 존재하지 않습니다.")
|
|
|
|
try:
|
|
with open(list_file, 'r', encoding='utf-8') as f:
|
|
return [line.strip() for line in f.readlines() if line.strip()]
|
|
except FileNotFoundError:
|
|
print(f"'{list_file}' 파일이 존재하지 않습니다.")
|
|
return []
|
|
|
|
# 특정 폴더에서 파일을 검색하고 압축하는 함수
|
|
def zip_selected_files(folder_path, file_list, output_zip):
|
|
with zipfile.ZipFile(output_zip, 'w') as zipf:
|
|
for file_name in file_list:
|
|
# 확장자를 .txt로 고정
|
|
file_name_with_ext = f"{file_name}.txt"
|
|
|
|
file_path = os.path.join(folder_path, file_name_with_ext)
|
|
if os.path.exists(file_path):
|
|
print(f"압축 중: {file_name_with_ext}")
|
|
zipf.write(file_path, arcname=file_name_with_ext)
|
|
else:
|
|
print(f"파일을 찾을 수 없거나 지원되지 않는 파일 형식입니다: {file_name_with_ext}")
|
|
print(f"완료: '{output_zip}' 파일이 생성되었습니다.")
|
|
|
|
# /app/idrac_info/backup/ 폴더 내 폴더를 나열하고 사용자 선택 받는 함수
|
|
def select_folder():
|
|
base_path = "/data/app/idrac_info/data/backup/"
|
|
if not os.path.isdir(base_path):
|
|
raise ValueError(f"기본 경로 '{base_path}'이(가) 존재하지 않습니다.")
|
|
|
|
folders = [f for f in os.listdir(base_path) if os.path.isdir(os.path.join(base_path, f))]
|
|
if not folders:
|
|
raise ValueError(f"'{base_path}'에 폴더가 존재하지 않습니다.")
|
|
|
|
print("사용 가능한 폴더:")
|
|
for idx, folder in enumerate(folders, start=1):
|
|
print(f"{idx}. {folder}")
|
|
|
|
choice = int(input("원하는 폴더의 번호를 선택하세요: ").strip())
|
|
if choice < 1 or choice > len(folders):
|
|
raise ValueError("올바른 번호를 선택하세요.")
|
|
|
|
return os.path.join(base_path, folders[choice - 1])
|
|
|
|
# 주요 실행 코드
|
|
if __name__ == "__main__":
|
|
try:
|
|
# /app/idrac_info/backup/ 폴더 내에서 폴더 선택
|
|
folder_path = select_folder()
|
|
|
|
output_zip_name = input("생성할 zip 파일명을 입력하세요 (확장자 제외, 예: output): ").strip()
|
|
|
|
# zip 파일 경로를 현재 디렉토리로 설정
|
|
output_zip = os.path.join(os.getcwd(), f"{output_zip_name}.zip")
|
|
|
|
# 파일명 리스트 가져오기
|
|
file_list = read_file_list()
|
|
|
|
if not file_list:
|
|
print("list.txt에 파일명이 없습니다.")
|
|
else:
|
|
zip_selected_files(folder_path, file_list, output_zip)
|
|
except ValueError as e:
|
|
print(e)
|