2014-10-31 2 views
-1

몇 개의 큰 파일을 파이썬 3을 사용하여 1로 병합 할 수 있습니까? 너무 많은 RAM을 사용 그것은 bash는 명령파이썬 3을 사용하여 큰 바이너리 파일 병합 3

cat * > outfile 

처럼 작동해야하지만, 리눅스, 윈도우와 내가

outfile = open("outfile", "wb") 
for file in os.listdir(): 
    outfile.write(file.read()) 

를 사용하는 경우 OS X를

에서 작동해야

+0

"대용량 파일"을 사용해 보셨습니까? http://stackoverflow.com/a/13613375/3430986? – orestisf

+0

텍스트 파일에만 작동합니다. –

+0

[파이썬 연결 텍스트 파일] (http://stackoverflow.com/questions/13613336/python-concatenate-text-files) – inspectorG4dget

답변

1

대형 이진 파일의 경우, 줄을 읽는 대신 디스크 블록 크기의 배수 인 청크를 읽습니다. 뭔가 (테스트되지 않은)

BLOCKSIZE = 4096 # typical, I believe 
BLOCKS = 1024 # somewhat arbitrary 
chunk = BLOCKS * BLOCKSIZE 
with open("outfile", "wb") as outfile: 
    for fname in os.listdir(): 
     with open('fname', "rb") as infile 
      outfile.write(infile.read(chunk)) 
관련 문제