오음

python) os 모듈 활용 본문

데이터 엔지니어링/파이썬

python) os 모듈 활용

오준돌 2023. 3. 6. 22:53
import os

os.getcwd() # 경로확인
'c:\\workspace'

os.mkdir("test") # 파일생성

# 절대 경로 -> c:\\workspace\\test
# 상대 경로 -> ./test

os.listdir("./") # 모든 파일 리스트 검색
os.walk("c:/") # 하위 디렉토리 검색

# 모든 파일에서 .py 파일만 뽑기
path = "./"
file_list = os.listdir(path)
file_list_py = [file for file in file_list if file.endswith(".py")]

print (file_list_py)

# 현재 경로에 test폴더 있는지 확인
os.path.isdir("./test")
True

# 경로 바꾸기
os.chdir

# 파일 check하고 생성
if os.path.isdir("./data") == False:
    os.mkdir("./data")