파이썬 바이너리 파일 읽을 때, 파일 끝까지 반복 하려면 어떻게 해야 하나요?

조회수 431회

파이썬 파일을 바이너리 모드로 읽을 때 파일 끝까지 반복 하려면 어떻게 해야 하나요? ​

f = open(file, 'rb')

fw = open(file1, 'w')

temp = f.read(10)

slen_temp = f.lead(4)

slen = int.from_bytes(slen_temp, byteorder='little')

sname_temp = f.lead(slen)

sname = sname_temp.decode('utf-8')

fw.write(sname + '\n')

굵게 처리 된 부분을 파일 끝까지 반복 시키려면 어떻게 해야 하나요?

그리고 반복 해야 될 부분을 좀더 간결 하게 수정 할 수 있나요?

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • f = open(file0, 'rb')
    fw = open(file1, 'w')
    
    while True:
        temp = f.read(10)
    
        if len(temp) == 0:
            break
    
        slen_temp = f.read(4)
    
        slen = int.from_bytes(slen_temp, byteorder='little')
    
        sname = f.read(slen)
    
        sname = sname.decode('utf-8')
    
        fw.write(sname + "\n"))
    
    f.close()
    fw.close()
    

    대략 이렇게 하면 될 겁니다. 무한루프로 반복하고, 나가는 조건 파일에 맞게 조정하면 되겠죠.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)