분류 전체보기 64

python :: 파이썬 파일 복사 shutil.copy 속도 빠르게 향상시키기

파이썬에서 파일을 복사할 때 shutil 라이브러리의 copy, copyfile 등의 함수를 많이 사용한다. 그런데 파일 복사 속도가 꽤 느린 편이다^.^; shutil 라이브러리 파일에서 copy 함수들을 보면 궁극적으로 copyfileobj 함수를 호출하게 되어 있다. def copyfileobj(fsrc, fdst, length=16*1024): """copy data from file-like object fsrc to file-like object fdst""" while 1: buf = fsrc.read(length) if not buf: break fdst.write(buf) copyfileobj 함수는 이렇게 정의되어있다. 주목해야 할 부분은 length=16*1024 즉, 파일 전송 시 ..

:: python 2021.06.17

python :: 파이썬 파일/폴더 복사 shutil copy, copy2, copyfile, copytree

파이썬 shutil 라이브러리에는 파일/폴더 복사와 관련된 여러 함수가 있다. copy, copy2, copyfile, copytree (copy2 라니.. 공식 라이브러리인데 너무 대충 만든거 아닌지ㅋㅋㅋ) 각각의 기능을 살펴보면 함수명 기능 사용방법 copy 파일을 복사한다 shutil.copy(src 파일 경로, dest 파일(or 폴더) 경로) * dest 에 폴더 경로를 지정할 경우 src 파일명과 같은 파일 생성 copy2 파일을 복사한다 shutil.copy2(src 파일 경로, dest 파일(or 폴더) 경로) * dest 에 폴더 경로를 지정할 경우 src 파일명과 같은 파일 생성 copyfile 파일을 복사한다 shutil.copyfile(src 파일 경로, dest 파일 경로) cop..

:: python 2021.06.16

python :: 파이썬 zipfile 로 파일 압축하기(하위폴더 포함/미포함) & 압축 해제하기

파이썬에서 파일 압축 & 압축 해제는 zipfile 라이브러리를 사용한다. C:\test └ directory1 └ file4.txt └ file1.txt └ file2.txt └ file3.txt 위 구조에서 C:\test 아래에 있는 txt 파일들을 압축해보기로 한다. 이 때, test 폴더 안의 txt 파일만 압축하려면 os.listdir 을, test 폴더 내 모든 하위폴더 아래에 있는 txt 파일을 다 압축하려면 os.walk 를 사용한다. * 참고: https://toramko.tistory.com/2 python * os.listdir과 os.walk (파이썬 특정 경로 내 디렉토리와 파일 검색) 다음과 같이 폴더와 파일을 생성해두었다. C:\test └ directory1 └ file4.t..

:: python 2021.06.15

python :: os.listdir과 os.walk (파이썬 특정 경로 내 디렉토리와 파일 검색)

다음과 같이 폴더와 파일을 생성해두었다. C:\test └ directory1 └ file4.txt └ file1.txt └ file2.txt └ file3.txt 1. os.listdir os.listdir(path) 특정 경로 내에 존재하는 폴더(디렉토리)와 파일 리스트를 검색 (1-depth) import os file_path = 'C:\\test' for file in os.listdir(file_path): print(file) 위와 같이 작성한 후 실행시키면 directory1 file1.txt file2.txt file3.txt 이렇게 출력된다. directory1 폴더 아래의 file4.txt 는 출력되지 않는다. 2. os.walk os.walk(path) 특정 경로 내에 존재하는 폴더..

:: python 2021.06.14
300x250