SHORTCUT
반응형

⚙
개발 환경
- device :
2021 Macbook Pro M1 Pro 14inch
(맥북프로 14 M1 Pro 모델)
- OS : macOS Ventura 13.2
⚠️
본 포스팅은 저의 개인적 생각을 기반으로 작성되었습니다.
따라서 정답이 아닌, 에러 해결을 위한 하나의 과정으로서 참고하시길 바랍니다.
*수정이 필요한 부분이 있을 경우, 댓글로 알려 주시면 수정하도록 하겠습니다😄
📌 에러 내용 및 상황
- zip파일의 압축을 해제하기 위해서,
zip.extractall(zip_folder)
기능을 사용하고 있음
- 그런데 압축 폴더명도 길고, 파일명도 길어서 아래의 에러가 발생하고 있음
OSError: [Errno 63] File name too long: {파일경로}/{파일명.zip}
👿 원인
- 위 에러는 python에서 파일 경로와 파일 이름이 결합하여 너무 길어지는 경우 지나치게 긴 경우 발생함
- 대부분의 운영 체제에서는 파일 이름의 최대 길이를 제한하고 있으며, 이를 초과하면 파일 이름이 너무 길다는 오류를 발생시킴
🚨
::결론::
너무 긴 파일명!
🤩 해결
파일명 혹은 파일 경로를 단축하여, 길지 않게 바꿔주면 된다. 다음은 파이썬에서 해결할 수 있도록 간단하게 작성해본 기능이다.
파이썬 코드
def modify_zip_filenames(zip_file_path):
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
for member in zip_ref.infolist():
# 파일 이름이 255자를 초과하는지 확인
if len(member.filename) > 255:
# 파일 이름을 255자로 수정
modified_filename = member.filename[:255]
member.filename = modified_filename
yield member, zip_ref
적용
def exportZip(download_dir='~/'):
# zip 파일로 다운로드 한 경우 압축해제
pages_zip = glob(get_dir(os.path.join(download_dir, 'Export*.zip')))
for zip_fp in pages_zip:
zip = zipfile.ZipFile(zip_fp)
# 압축할 폴더 생성
zip_folder = get_dir(os.path.join(download_dir, zip_fp.replace('.zip', '')))
os.makedirs(zip_folder, exist_ok=True)
# 압축 풀기
for member, zip_ref in modify_zip_filenames(zip_fp):
zip_ref.extract(member, zip_folder)
# zip 파일 제거
os.remove(zip_fp)
📖 레퍼런스
반응형