WHY USE BINARY MODE? |
왜 2진모드를 써야 할까요? |
If you use binary mode when you read (or write) a file, things won’t be much different. |
당신이 파일을 읽을 때(또는 쓸 때) 2진모드를 쓴다고 해도 많이 달라지는 것은 없을 것입니다. |
You are still able to read a number of bytes (basically the same as characters), and perform other operations associated with text files. |
당신은 아직 바이트 수를 읽어낼 수 있을 것이며(기본적으로 문자와 같습니다), 텍스트 파일과 관련한 다른 작업들도 실행할 수 있습니다. |
The main point is that when you use binary mode, Python gives you exactly the contents found in the file— and in text mode, it won’t necessarily do that. |
당신이 2진모드를 사용할 때 중요한 점은 파이썬은 파일에서 발견되는 내용을 정확히 제공한다는 점과—텍스트 모드에서는 그렇게 할 필요가 없다는 점 입니다. |
If you find it shocking that Python manipulates your text files, don’t worry. |
만약 파이썬이 당신의 텍스트 파일을 조작하는 걸 보고 충격적이었다면 걱정하지 마세요. |
The only “trick” it employs is to standardize your line endings. |
파이썬이 채택한 단 한 개의 "트릭"은 당신의 명령줄 끝단을 표준화하는 것입니다. |
Generally, in Python, you end your lines with a newline character ( |
일반적으로 파이썬에서 당신은 명령줄의 끝부분을 유닉스 시스템의 규칙과 같은 개행 문자(/n)로 마무리합니다. |
This is not standard in Windows, however. |
하지만 이것은 윈도우 시스템에서는 쓰일 수 없습니다. |
In Windows, a line ending is marked with |
윈도우 시스템에서는 명령줄 끝단은 /r/n으로 처리됩니다. |
. |
당신 프로그램에서 이것을 숨기기 위해서(그래서 각기 다른 플랫폼에서 매끄럽게 작동되게 하기 위하여), 파이썬은 여기서 몇가지 자동화된 변환을 수행합니다. |
When you read text from a file in text mode in Windows, it converts |
당신이 윈도우 텍스트 모드에서 파일로부터 텍스트를 읽을 때, /r/n은 /n으로 변환됩니다. |
. Conversely, when you write text to a file in text mode in Windows, it converts |
반대로, 당신이 윈도우 텍스트 모드에서 파일에 텍스트를 입력할 때 /n은 /r/n으로 변환됩니다. |
to |
(맥킨토시 버젼도 동일하게 수행되지만 /n을 /r로 변환합니다.) |
(The Macintosh version does the same thing, but converts between |
문제는 당신이 2진 파일로 작업할 때 사운드 클립과 같은 형태로 일어납니다. |
.) |
이전 단에서 언급 되었듯이 그것은 명령줄 끝문자로 해석된 바이트들을 포함할 것입니다. |
It may contain bytes that can be interpreted as the line-ending characters mentioned in the previous paragraph, and if you are using text mode, Python performs its automatic conversion. |
그리고 만약 당신이 텍스트 모드를 사용하고 있다면, 파이썬은 자체의 자동 변환을 수행합니다. |
However, that will probably destroy your binary data. |
하지만 그것은 아마도 당신의 2진 데이터를 파괴할 것입니다. |
So, to avoid that, you simply use binary mode, and no conversions are made. |
그래서 그것을 방지하기 위하여 당신이 단순히 2진 모드를 사용하면 아무런 변환도 생성되지 않을 것입니다. |
*Source: "Beginning Python |
*출처: "Beginning Python |
From Novice to Professional, Second Edition", Copyright © 2008 by Magnus Lie Hetland. |
From Novice to Professional, Second Edition", Copyright © 2008 by Magnus Lie Hetland. |