Notice
Recent Posts
Recent Comments
Link
뭐라도 배우면 기록하자
[Python] 폴더 안의 파일 이름 읽고 쓰는 프로그램 본문
특정 폴더 내에 있는 수 많은 파일 이름을 정리해야 하는 일이 생겨서 구글링을 통해 아래의 코드를 짜봤습니다.
-----------------------------------------------------------------------------------------------------------------------------------
import os
path = os.getcwd() #현재 폴더의 경로 읽기
file_list = os.listdir(path) #읽어드린 경로에 있는 전체 파일 리스트 읽기
nfile = open("filenames_in_folder.txt","w",encoding='UTF-8') #읽은 파일리스트를 텍스트 파일로 쓰는 부분
for f in file_list:
if f == "filename.py" or f == "filename.exe":
continue
nfile.write(f+"\n")
nfile.close()
-----------------------------------------------------------------------------------------------------------------------------------
nfile = open("filenames_in_folder.txt","w",encoding='UTF-8')에서 encoding='UTF-8'을 넣지 않으면 인코딩 에러가 나더군요. 인코딩의 중요성을 한번 더 깨닫습니다.
'프로그래밍언어공부 > Python' 카테고리의 다른 글
[Python] exe 파일 만들기 (0) | 2020.04.12 |
---|
Comments