본문 바로가기
나를 위한 코드

파이썬 폴더 내 파일 모두 삭제하기

by 라나나 2023. 4. 17.
728x90
import os

folder_path = os.path.join(os.getcwd(), 'downloads')

for filename in os.listdir(folder_path):
    file_path = os.path.join(folder_path, filename)
    try:
        if os.path.isfile(file_path):
            os.unlink(file_path)
    except Exception as e:
        print(f"Failed to delete {file_path} due to {e}")
728x90

댓글