import os
import tarfile
from datetime import datetime
def create_tar_without_spaces(src_dir, tar_path):
# 디렉토리가 없으면 생성
os.makedirs(os.path.dirname(tar_path), exist_ok=True)
files_to_add = []
for root, _, files in os.walk(src_dir):
for file in files:
if ' ' in file:
continue # 공백 포함된 파일 제외
full_path = os.path.join(root, file)
rel_path = os.path.relpath(full_path, src_dir)
files_to_add.append((full_path, rel_path))
with tarfile.open(tar_path, "w") as tar:
for full_path, rel_path in files_to_add:
tar.add(full_path, arcname=rel_path)
print(f"✅ Created tar file: {tar_path} with {len(files_to_add)} files (공백 제외)")
if __name__ == "__main__":
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
src_directory = "D:\\src\\src\\main"
output_tar = f"D:\\tmp\\src-backup\\backup-{timestamp}.tar"
create_tar_without_spaces(src_directory, output_tar)