본문 바로가기

프로그래밍

Python OS module 정리

import os

os.listdir(path) path의 모든 파일/폴더을 보여줍니다. 
os.walk(path) path안의 모든 파일에 대하여 디렉토리로 묶어줍니다. 
os.getcwd() 현재 작업 공간을 보여줍니다. 
os.chdir(path) path로 작업 공간을 바꿉니다.

 

현재 작업공간의 Sub directory까지 모든 파일을 탐색하는 방법

주의사항: 내부적으로 파일이 너무 많은 위치에서 사용시 탐색을 엄청 많이 합니다.

ex) os.walk('C:') 엄청 오래 시간 걸려요.

for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))
 
wefwefwe

vdv3C:/

파이썬에서 파일을 다뤄보자

 

os.path

os.path.exists(path) path가 존재하는지 여부를 확인해준다.
os.path.dirname(path) path의 디렉토리 이름만 알려준다. 
os.path.basename(path) path의 파일 이름만 알려준다. 
os.path.abspath(path) 상대주소를 절대주소로 바꿔준다. 
os.path.split(path) path의 (디렉토리 이름, 파일이름) 알려준다. 
os.path.splitext(path) path의 (파일이름, 확장자)를 알려준다.