파일 조작과 시스템 2

파일 조작과 시스템

파일 조작 - 라이브러리

"""
import os

# 파일이 존재하나요 ?
print(os.path.exists('test.txt'))

# 다음이 파일인가요 ?
print(os.path.isfile('test.txt'))

# 다음이 폴더인가요 ?
print(os.path.isdir('test.txt'))

# 파일의 이름 변경
os.rename('test.txt', 'rename.txt')

# 파일끼리 링크걸기(내용 공유)
os.symlink('rename.txt', 'symlink.txt')

# 폴더 생성
os.mkdir('test_dir')

# 폴더 삭제
os.remove('test_dir')
* 폴더 안에 파일 있으면 삭제 불

# 폴더 안에 폴더 검색해서 리스트로 돌려줌
print(os.listdir('test_dir'))

# 현재 위치 출력
print(os.getcwd())

# 폴더 안에 파일 검색해서 리스트로 돌려줌
import glob
print(glob.glob('test_dir/*'))
* 는 전부 표시

# 파일 복사
import shutil
shutil.copy('test_dir/복사할 파일명', 'test_dir/붙여넣기할 파일명')

# 파일이 존재하는 폴더 삭제
import shutil
shutil.rmtree('test_dir')

좋은 웹페이지 즐겨찾기