[python] os 관련 함수

os.path.join 경로를 합칠 수 있음

import os

#데이터 읽기
directory ="./imdb dataset"
x=[]; y=[]
for c in ['neg', 'pos']:
    curr =os.path.join(directory,c)
    print(curr)

os.listdir("경로") 경로내의 모든 파일과 디렉토리 리스트를 반환

for filename in os.listdir("/content/test_images"):
    if 'jpg' not in filename:
        continue

os.chdir 해당 경로로 이동

os.chdir('/content/test_images')

os.isfile 파일이 존재하는가?

os.remove 해당 파일 지우기

if os.path.isfile('word.mp3'): os.remove('word.mp3')

word.mp3파일이 존재한다면 지우기

 

os.isdir 디렉토리가 존재하는가?

os.mkdir 디렉토리 만들기

os.chdir('/content/test_images')
if not os.path.isdir('class_buckets'):
    os.mkdir('class_buckets')

위 경우엔 content/test_images 경로로 이동해서

class_buckets라는 디렉토리가 존재하지 않는다면 class_buckets디렉토리 생성