티스토리 뷰

개발/파이썬

파일 열 때 't' 옵션의 용도

맨날치킨 2023. 1. 21. 10:05
반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Open files in 'rt' and 'wt' modes

'rt' 및 'wt' 모드로 파일 열기

 문제 내용 

Several times here on SO I've seen people using rt and wt modes for reading and writing files.

여러 번 SO에서 파일을 읽고 쓰기 위해 rt 및 wt 모드를 사용하는 것을 본 적이 있습니다.

 

For example:

예를 들어:
with open('input.txt', 'rt') as input_file:
     with open('output.txt', 'wt') as output_file: 
         ...

 

I don't see the modes documented, but since open() doesn't throw an error - looks like it's pretty much legal to use.

모드가 문서화되어 있지 않지만, open()이 오류를 throw하지 않기 때문에 사용하는 것이 거의 합법적인 것 같습니다.

 

What is it for and is there any difference between using wt vs w and rt vs r?

이것이 무엇을 위한 것이며 wt 대신 w 및 rt 대신 r을 사용하는 것에 어떤 차이가 있는지 무엇인가요?

 

 

 

 높은 점수를 받은 Solution 

t refers to the text mode. There is no difference between r and rt or w and wt since text mode is the default.

t는 텍스트 모드를 나타냅니다. 텍스트 모드는 기본값이므로 r과 rt 또는 w와 wt에 차이가 없습니다.

 

Documented here:

여기에 문서화되어 있습니다:
Character   Meaning
'r'     open for reading (default)
'w'     open for writing, truncating the file first
'x'     open for exclusive creation, failing if the file already exists
'a'     open for writing, appending to the end of the file if it exists
'b'     binary mode
't'     text mode (default)
'+'     open a disk file for updating (reading and writing)
'U'     universal newlines mode (deprecated)

 

The default mode is 'r' (open for reading text, synonym of 'rt').

기본 모드는 'r'(텍스트 읽기용으로 열기,'rt'의 동의어)입니다.

 

 

 

 가장 최근 달린 Solution 

The t indicates text mode, meaning that \n characters will be translated to the host OS line endings when writing to a file, and back again when reading. The flag is basically just noise, since text mode is the default.

t는 텍스트 모드를 나타내며, 파일에 쓸 때 \n 문자는 호스트 OS 줄 바꿈으로 번역되고, 읽을 때는 다시 돌아옵니다. 이 플래그는 기본적으로 노이즈에 불과합니다. 텍스트 모드가 기본값이기 때문입니다.

 

Other than U, those mode flags come directly from the standard C library's fopen() function, a fact that is documented in the sixth paragraph of the python2 documentation for open().

U를 제외한 다른 모드 플래그는 표준 C 라이브러리의 fopen () 함수에서 직접 가져온 것입니다. 이 사실은 open ()의 python2 설명서의 여섯 번째 단락에 문서화되어 있습니다.

 

As far as I know, t is not and has never been part of the C standard, so although many implementations of the C library accept it anyway, there's no guarantee that they all will, and therefore no guarantee that it will work on every build of python. That explains why the python2 docs didn't list it, and why it generally worked anyway. The python3 docs make it official.

내가 아는 한, t는 C 표준의 일부가 아니며, 오히려 많은 C 라이브러리 구현이 그것을 허용하기 때문에 모든 빌드에서 작동할 것이라는 보장이 없습니다. 그래서 python2 문서에서는 이것을 나열하지 않았으며, 그러면 대부분 작동했습니다. python3 문서는 이것을 공식적으로 만듭니다.

 

 

 

출처 : https://stackoverflow.com/questions/23051062/open-files-in-rt-and-wt-modes

반응형
댓글
공지사항
최근에 올라온 글