현재 폴더 확인하기

import os

os.getcwd()

 

현재 폴더 바꾸기

os.chdir("users/myname/hi")

 

폴더에 있는 파일 가져오기

os.listdir("/users/myname")

 

특정 확장자만 확인하기

import glob

glob.glob('./*.csv')

 

하위 폴더까지 확인하기

for root, dirs, files in os.walk(os.getcwd()):

    print(files)

 

특정 확장자 파일만 확인하기

for root, dirs, files in os.walk(os.getcwd()):

    for f in files:

        a = os.path.splitext(f)[-1]

        if a == '.xml':

            print(f)

 

파일, 폴더 존재 확인하기

os.path.exists('/users/myname')

os.path.isdir('/users/myname')

os.path.isfile('/users/myname/test.csv')

 

파일 복사하기

import shutil

shutil.copytree('dirname1', 'dirname2')

 

파일 이동하기

shutil.move('filename1.txt', 'filename2.txt')

 

폴더 삭제하기

shutil.rmtree('foldername')

 

파일 삭제하기

os.remove('test.csv')

 

https://wikidocs.net/21049

 

01_파일, 디렉터리 관리하기

파이썬의 장점 중 하나는 일률적인 업무를 돕는 프로그램을 쉽게 만들 수 있다는 점이다. 이런 것이 가능한 이유는 파일과 디렉터리를 쉽게 관리할 수 있기 때문이다. 파일, ...

wikidocs.net

 

+ Recent posts