io â ì¤í¸ë¦¼ ìì
ì ìí íµì¬ ë구¶
ìì¤ ì½ë: Lib/io.py
ê°ì¶
The io module provides Pythonâs main facilities for dealing with various
types of I/O. There are three main types of I/O: text I/O, binary I/O
and raw I/O. These are generic categories, and various backing stores can
be used for each of them. A concrete object belonging to any of these
categories is called a file object. Other common terms are stream
and file-like object.
ë²ì£¼ì ìê´ìì´, ê° êµ¬ì ì¤í¸ë¦¼ ê°ì²´ìë ë¤ìí 기ë¥ì´ ììµëë¤: ì½ê¸° ì ì©, ì°ê¸° ì ì© ëë ì½ê³ -ì°ê¸°ì¼ ì ììµëë¤. ëí ììì 무ìì ì¡ì¸ì¤(ììì ìì¹ë¡ ì ë°©ì´ë íë°© íì)를 íì©íê±°ë ìì°¨ì ì¡ì¸ì¤(ì를 ë¤ì´ ìì¼ì´ë íì´íì ê²½ì°)ë§ íì©í ì ììµëë¤.
모ë ì¤í¸ë¦¼ì ê·¸ê²ì ì ê³µíë ë°ì´í°íì 주ì를 기ì¸ì
ëë¤. ì를 ë¤ì´ str ê°ì²´ë¥¼ ë°ì´ë리 ì¤í¸ë¦¼ì write() ë©ìëì ì ê³µíë©´ TypeErrorê° ë°ìí©ëë¤. í
ì¤í¸ ì¤í¸ë¦¼ì write() ë©ìëì bytes ê°ì²´ë¥¼ ì ê³µí´ë ë§ì°¬ê°ì§ì
ëë¤.
ë²ì 3.3ìì ë³ê²½: IOErrorê° ì´ì ë OSErrorì ë³ì¹ì´ë¼ì, IOError를 ë°ììì¼°ë ì°ì°ì ì´ì OSError를 ë°ììíµëë¤.
í ì¤í¸ I/O¶
í
ì¤í¸ I/Oë str ê°ì²´ë¥¼ 기ëíê³ ìì±í©ëë¤. ì´ë ë°°ê²½ ì ì¥ìê° ë¤ì´í°ë¸ íê² ë°ì´í¸ì´ë¡ 구ì±ëìì ëë§ë¤ (ê°ë ¹ íì¼ì ê²½ì°), íë«í¼ë³ ì¤ ëê¹ ë¬¸ìì ì íì ë³íë¿ë§ ìëë¼ ë°ì´í°ì ì¸ì½ë©ê³¼ ëì½ë©ì´ í¬ëª
íê² ì´ë£¨ì´ì§ì ì미í©ëë¤.
í
ì¤í¸ ì¤í¸ë¦¼ì ë§ëë ê°ì¥ ì¬ì´ ë°©ë²ì open()ì ì¬ì©íë ê²ì´ê³ , ì íì ì¼ë¡ ì¸ì½ë©ì ì§ì í©ëë¤:
f = open("myfile.txt", "r", encoding="utf-8")
ì¸ ë©ëª¨ë¦¬ í
ì¤í¸ ì¤í¸ë¦¼ë StringIO ê°ì²´ë¡ ì ê³µë©ëë¤:
f = io.StringIO("some initial text data")
ì°¸ê³
When working with a non-blocking stream, be aware that read operations on text I/O objects
might raise a BlockingIOError if the stream cannot perform the operation
immediately.
í
ì¤í¸ ì¤í¸ë¦¼ APIë TextIOBaseì ì¤ëª
ìì ìì¸í ì¤ëª
ëì´ ììµëë¤.
ë°ì´ë리 I/O¶
ë°ì´ë리 I/O(ë²í¼ë§ ë(buffered) I/Oë¼ê³ ë í©ëë¤)ë ë°ì´í¸ì´ë¥ ê°ì²´ë¥¼ 기ëíê³ bytes ê°ì²´ë¥¼ ìì±í©ëë¤. ì¸ì½ë©, ëì½ë© ëë ì¤ ëê¹ ë³íì´ ìíëì§ ììµëë¤. ì´ ë²ì£¼ì ì¤í¸ë¦¼ì 모ë ì¢
ë¥ì í
ì¤í¸ê° ìë ë°ì´í°ì ì¬ì©í ì ìì¼ë©°, í
ì¤í¸ ë°ì´í° ì²ë¦¬ë¥¼ ìëì¼ë¡ ì ì´í´ì¼ í ëë ì¬ì©í ì ììµëë¤.
ë°ì´ë리 ì¤í¸ë¦¼ì ë§ëë ê°ì¥ ì¬ì´ ë°©ë²ì 모ë 문ìì´ì 'b'를 ì ê³µíì¬ open()ì ì¬ì©íë ê²ì
ëë¤:
f = open("myfile.jpg", "rb")
ì¸ ë©ëª¨ë¦¬ ë°ì´ë리 ì¤í¸ë¦¼ë BytesIO ê°ì²´ë¡ ì ê³µë©ëë¤:
f = io.BytesIO(b"some initial binary data: \x00\x01")
ë°ì´ë리 ì¤í¸ë¦¼ APIë BufferedIOBase ì¤ëª
ìì ìì¸í ì¤ëª
ëì´ ììµëë¤.
ë¤ë¥¸ ë¼ì´ë¸ë¬ë¦¬ 모ëì í
ì¤í¸ë ë°ì´ë리 ì¤í¸ë¦¼ì ë§ëë ë¤ë¥¸ ë°©ë²ì ì ê³µí ì ììµëë¤. ì를 ë¤ì´ socket.socket.makefile()ì 참조íììì¤.
ìì I/O¶
ìì I/O(ë²í¼ë§ ëì§ ìì(unbuffered) I/Oë¼ê³ ë í©ëë¤)ë ì¼ë°ì ì¼ë¡ ë°ì´ë리ì í ì¤í¸ ì¤í¸ë¦¼ì ìí ì ìì¤ ë¹ë© ë¸ë¡ì¼ë¡ ì¬ì©ë©ëë¤; ì¬ì©ì ì½ëìì ìì ì¤í¸ë¦¼ì ì§ì ì¡°ìíë ê²ì ê±°ì ì ì©íì§ ììµëë¤. ê·¸ë°ë°ë, ë²í¼ë§ì ë¹íì±íí´ì ë°ì´ë리 모ëë¡ íì¼ì ì´ì´ ìì ì¤í¸ë¦¼ì ë§ë¤ ì ììµëë¤:
f = open("myfile.jpg", "rb", buffering=0)
ìì ì¤í¸ë¦¼ APIë RawIOBase ì¤ëª
ìì ìì¸í ì¤ëª
ëì´ ììµëë¤.
í ì¤í¸ ì¸ì½ë©Â¶
The default encoding of TextIOWrapper and open() is
locale-specific (locale.getencoding()).
However, many developers forget to specify the encoding when opening text files encoded in UTF-8 (e.g. JSON, TOML, Markdown, etcâ¦) since most Unix platforms use UTF-8 locale by default. This causes bugs because the locale encoding is not UTF-8 for most Windows users. For example:
# íì¼ì ë¹ ASCII 문ìê° ìì ë ìëì°ìì ìëíì§ ìì ì ììµëë¤.
with open("README.md") as f:
long_description = f.read()
Accordingly, it is highly recommended that you specify the encoding
explicitly when opening text files. If you want to use UTF-8, pass
encoding="utf-8". To use the current locale encoding,
encoding="locale" is supported since Python 3.10.
ë 보기
- Python UTF-8 Mode
Python UTF-8 Mode can be used to change the default encoding to UTF-8 from locale-specific encoding.
- PEP 686
Python 3.15 will make Python UTF-8 Mode default.
Opt-in EncodingWarning¶
Added in version 3.10: See PEP 597 for more details.
To find where the default locale encoding is used, you can enable
the -X warn_default_encoding command line option or set the
PYTHONWARNDEFAULTENCODING environment variable, which will
emit an EncodingWarning when the default encoding is used.
If you are providing an API that uses open() or
TextIOWrapper and passes encoding=None as a parameter, you
can use text_encoding() so that callers of the API will emit an
EncodingWarning if they donât pass an encoding. However,
please consider using UTF-8 by default (i.e. encoding="utf-8") for
new APIs.
ê³ ìì¤ ëª¨ë ì¸í°íì´ì¤Â¶
- io.DEFAULT_BUFFER_SIZE¶
모ëì ë²í¼ë§ ë I/O í´ëì¤ìì ì¬ì©íë 기본 ë²í¼ í¬ê¸°ë¥¼ í¬í¨íë int.
open()ì ê°ë¥íë©´ íì¼ì blksize(os.stat()ì¼ë¡ ì»ì)를 ì¬ì©í©ëë¤.
- io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)¶
ì´ê²ì ë´ì¥
open()í¨ìì ë³ì¹ì ëë¤.ì´ í¨ìë ì¸ì path, mode ë° flagsë¡ ê°ì¬ ì´ë²¤í¸
openì ë°ììíµëë¤. modeì flags ì¸ìë ìë í¸ì¶ìì ìì ëìê±°ë ì ì¶ëìì ì ììµëë¤.
- io.open_code(path)¶
ì ê³µë íì¼ì
'rb'모ëë¡ ì½ëë¤. ì´ í¨ìë ë´ì©ì ì¤í ì½ëë¡ ì·¨ê¸íë ¤ê³ í ë ì¬ì©í´ì¼ í©ëë¤.pathë
strì´ê³ ì ë ê²½ë¡ì¬ì¼ í©ëë¤.ì´ í¨ìì ëìì
PyFile_SetOpenCodeHook()ì ëí ì´ì í¸ì¶ë¡ ì¬ì ìë ì ììµëë¤. íì§ë§, pathê°strì´ê³ ì ë ê²½ë¡ìì ê°ì í ë,open_code(path)ë íìopen(path, 'rb')ì ê°ê² ëìí´ì¼ í©ëë¤. ëìì ì¬ì ìë íì¼ì ì¶ê° ì í¨ì± ê²ì¬ë ì¬ì ì²ë¦¬ë¥¼ ìí ê²ì ëë¤.Added in version 3.8.
- io.text_encoding(encoding, stacklevel=2, /)¶
This is a helper function for callables that use
open()orTextIOWrapperand have anencoding=Noneparameter.This function returns encoding if it is not
None. Otherwise, it returns"locale"or"utf-8"depending on UTF-8 Mode.This function emits an
EncodingWarningifsys.flags.warn_default_encodingis true and encoding isNone. stacklevel specifies where the warning is emitted. For example:def read_text(path, encoding=None): encoding = io.text_encoding(encoding) # stacklevel=2 with open(path, encoding) as f: return f.read()
In this example, an
EncodingWarningis emitted for the caller ofread_text().See í ì¤í¸ ì¸ì½ë© for more information.
Added in version 3.10.
ë²ì 3.11ìì ë³ê²½:
text_encoding()returns âutf-8â when UTF-8 mode is enabled and encoding isNone.
- exception io.BlockingIOError¶
ì´ê²ì ë´ì¥
BlockingIOErrorìì¸ì ëí í¸í ë³ì¹ì ëë¤.
- exception io.UnsupportedOperation¶
ì§ìëì§ ìë ì°ì°ì´ ì¤í¸ë¦¼ìì í¸ì¶ë ë ë°ìíë
OSErrorìValueError를 ììíë ìì¸.
ë 보기
sysíì¤ IO ì¤í¸ë¦¼ì í¬í¨í©ëë¤:
sys.stdin,sys.stdoutë°sys.stderr.
í´ëì¤ ìê³Â¶
I/O ì¤í¸ë¦¼ì 구íì í´ëì¤ì ìê³(hierarchy)ë¡ êµ¬ì±ë©ëë¤. 먼ì ë¤ìí ë²ì£¼ì ì¤í¸ë¦¼ì ì§ì íë ë° ì¬ì©ëë ì¶ì ë² ì´ì¤ í´ëì¤(ABC)ê° ìê³ , ê·¸ë¤ìì¼ë¡ íì¤ ì¤í¸ë¦¼ 구íì ì ê³µíë 구ì í´ëì¤ê° ììµëë¤.
ì°¸ê³
ì¶ì ë² ì´ì¤ í´ëì¤ë ëí 구ì ì¤í¸ë¦¼ í´ëì¤ì 구íì ë기 ìí´ ì¼ë¶ ë©ìëì 기본 구íì ì ê³µí©ëë¤. ì를 ë¤ì´, BufferedIOBaseë ìµì íëì§ ìì readinto()ì readline() 구íì ì ê³µí©ëë¤.
I/O ìê³ì 맨 ììë ì¶ì ë² ì´ì¤ í´ëì¤ IOBaseê° ììµëë¤. ì¤í¸ë¦¼ì ëí 기본 ì¸í°íì´ì¤ë¥¼ ì ìí©ëë¤. ê·¸ë¬ë ì¤í¸ë¦¼ì ëí ì½ê¸°ì ì°ê¸°ê° ë¶ë¦¬ëì§ ììì ì ìíììì¤; 구íì 주ì´ì§ ì°ì°ì ì§ìíì§ ìì¼ë©´ UnsupportedOperationì ë°ììí¬ ì ììµëë¤.
RawIOBase ABCë IOBase를 íì¥í©ëë¤. ì¤í¬ë¦¼ì ëí ë°ì´í¸ì´ì ì½ê¸°ì ì°ê¸°ë¥¼ ì²ë¦¬í©ëë¤. FileIOë RawIOBase를 ìë¸ í´ëì±íì¬ ê¸°ê³ì íì¼ ìì¤í
ì ìë íì¼ì ëí ì¸í°íì´ì¤ë¥¼ ì ê³µí©ëë¤.
BufferedIOBase ABCë IOBase를 íì¥í©ëë¤. ìì ë°ì´ë리 ì¤í¸ë¦¼(RawIOBase)ì ëí ë²í¼ë§ì ë¤ë£¹ëë¤. ì´ê²ì ìë¸ í´ëì¤, BufferedWriter, BufferedReader ë° BufferedRWPairë ê°ê° ì¸ ì ìë, ì½ì ì ìë, ê·¸ë¦¬ê³ ì½ê³ ì¸ ì ìë ìì ë°ì´ë리 ì¤í¸ë¦¼ì ë²í¼ë§í©ëë¤. BufferedRandomì íìí ì ìë (seekable) ì¤í¸ë¦¼ì ë²í¼ ì¸í°íì´ì¤ë¥¼ ì ê³µí©ëë¤. ë ë¤ë¥¸ BufferedIOBase ìë¸ í´ëì¤ BytesIOë ì¸ ë©ëª¨ë¦¬ ë°ì´í¸ì´ì ì¤í¸ë¦¼ì
ëë¤.
TextIOBase ABCë IOBase를 íì¥í©ëë¤. ì´ê²ì ë°ì´í¸ê° í
ì¤í¸ë¥¼ ëíë´ë ì¤í¸ë¦¼ì ë¤ë£¨ê³ , 문ìì´ê³¼ì ì¸ì½ë©ê³¼ ëì½ë©ì ì²ë¦¬í©ëë¤. TextIOBase를 íì¥íë TextIOWrapperë ë²í¼ë§ ë ìì ì¤í¸ë¦¼(BufferedIOBase)ì ëí ë²í¼ë§ ë í
ì¤í¸ ì¸í°íì´ì¤ì
ëë¤. ë§ì§ë§ì¼ë¡, StringIOë í
ì¤í¸ì ëí ì¸ ë©ëª¨ë¦¬ ì¤í¸ë¦¼ì
ëë¤.
ì¸ì ì´ë¦ì ëª
ì¸ì ì¼ë¶ê° ìëë©°, open()ì ì¸ìë í¤ìë ì¸ìë¡ë§ ì¬ì©íë ¤ë ìëì
ëë¤.
The following table summarizes the ABCs provided by the io module:
ABC |
ìì |
ì¤í (stub) ë©ìë |
ë¯¹ì¤ ì¸ ë©ìëì íë¡í¼í° |
|---|---|---|---|
|
|
||
|
ììë |
||
|
ììë |
||
|
ììë |
I/O ë² ì´ì¤ í´ëì¤Â¶
- class io.IOBase¶
모ë I/O í´ëì¤ì ì¶ì ë² ì´ì¤ í´ëì¤.
ì´ í´ëì¤ë íì í´ëì¤ê° ì íì ì¼ë¡ ì¬ì ìí ì ìë ë§ì ë©ìëì ëí´ ë¹ ì¶ì 구íì ì ê³µí©ëë¤; 기본 구íì ì½ê±°ë ì°ê±°ë íìí ì ìë íì¼ì ëíë ëë¤.
IOBaseê° ìëª ì´ ë¤ìí기 ë문ìread()ëwrite()를 ì ì¸íì§ ìëë¼ë, 구íê³¼ í´ë¼ì´ì¸í¸ë í´ë¹ ë©ìë를 ì¸í°íì´ì¤ì ì¼ë¶ë¡ ê³ ë ¤í´ì¼ í©ëë¤. ëí, ì§ìíì§ ìë ì°ì°ì´ í¸ì¶ë ë 구íìValueError(ëëUnsupportedOperation)를 ë°ììí¬ ì ììµëë¤.íì¼ìì ì½ê±°ë íì¼ì ì°ë ë°ì´ë리 ë°ì´í°ì ì¬ì©ëë 기본íì
bytesì ëë¤. ë¤ë¥¸ ë°ì´í¸ì´ë¥ ê°ì²´ë ë©ìë ì¸ìë¡ íì©ë©ëë¤. í ì¤í¸ I/O í´ëì¤ëstrë°ì´í°ë¡ ìëí©ëë¤.ë«í ì¤í¸ë¦¼ì ëí 모ë ë©ìë(ì¡°íì¡°ì°¨ë) í¸ì¶ì ì ìëì´ ìì§ ììµëë¤. ì´ ê²½ì° êµ¬íì
ValueError를 ë°ììí¬ ì ììµëë¤.IOBase(ë° ê·¸ ìë¸ í´ëì¤)ë ì´í°ë ì´í° íë¡í ì½ì ì§ìí©ëë¤. ì¦, ì¤í¸ë¦¼ìì ì¤ì ì°ì¶íë©´ìIOBaseê°ì²´ë¥¼ ì´í°ë ì´í¸ í ì ììµëë¤. ì¤í¸ë¦¼ì´ ë°ì´ë리 ì¤í¸ë¦¼(ë°ì´í¸ì´ì ì°ì¶í©ëë¤)ì¸ì§ í ì¤í¸ ì¤í¸ë¦¼(문ìì´ì ì°ì¶í©ëë¤)ì¸ì§ì ë°ë¼ ì¤ì ì½ê° ë¤ë¥´ê² ì ìë©ëë¤. ìëreadline()ì 참조íììì¤.IOBaseë 컨í ì¤í¸ ê´ë¦¬ìì´ê¸°ë í´ì,with문ì ì§ìí©ëë¤. ì´ ììì, fileìwith문ì ì¤ìí¸ê° ìë£ë íì ë«íëë¤ â ìì¸ê° ë°ìíëë¼ë:with open('spam.txt', 'w') as file: file.write('Spam and eggs!')
IOBaseë ë¤ì ë°ì´í° ì´í¸ë¦¬ë·°í¸ì ë©ìë를 ì ê³µí©ëë¤:- close()¶
ì´ ì¤í¸ë¦¼ì íë¬ìíê³ ë«ìµëë¤. íì¼ì´ ì´ë¯¸ ë«íì¼ë©´ ì´ ë©ìëë í¨ê³¼ê° ììµëë¤. ì¼ë¨ íì¼ì´ ë«íë©´, íì¼ì ëí 모ë ì°ì°(ì를 ë¤ì´ ì½ê¸°ë ì°ê¸°)ì´
ValueError를 ë°ììíµëë¤.í¸ìì, ì´ ë©ìë를 ë ë² ì´ì í¸ì¶í ì ììµëë¤; ê·¸ë¬ë 첫 ë²ì§¸ í¸ì¶ë§ í¨ê³¼ê° ììµëë¤.
- closed¶
ì¤í¸ë¦¼ì´ ë«íì¼ë©´
True.
- fileno()¶
ì¡´ì¬íë¤ë©´ ì¤í¸ë¦¼ì íë¶ íì¼ ê¸°ì ì(ì ì)를 ë°íí©ëë¤. IO ê°ì²´ê° íì¼ ê¸°ì ì를 ì¬ì©íì§ ìì¼ë©´
OSErrorê° ë°ìí©ëë¤.
- flush()¶
í´ë¹íë©´ ì¤í¸ë¦¼ì ì°ê¸° ë²í¼ë¥¼ íë¬ìí©ëë¤. ì´ê²ì ì½ê¸° ì ì©ê³¼ ë¹ ë¸ë¡í¹ ì¤í¸ë¦¼ì ëí´ìë ì무ê²ë íì§ ììµëë¤.
- isatty()¶
ì¤í¸ë¦¼ì´ ëííì´ë©´ (ì¦, í°ë¯¸ë/tty ì¥ì¹ì ì°ê²°ëìì¼ë©´)
True를 ë°íí©ëë¤.
- readable()¶
ì¤í¸ë¦¼ì ì½ì ì ìì¼ë©´
True를 ë°íí©ëë¤.Falseì´ë©´,read()ëOSError를 ë°ììíµëë¤.
- readline(size=-1, /)¶
ì¤í¸ë¦¼ìì í ì¤ì ì½ê³ ë°íí©ëë¤. sizeê° ì§ì ëë©´, ìµë size ë°ì´í¸ë¥¼ ì½ìµëë¤.
ì¤ ì¢ ê²°ìë ë°ì´ë리 íì¼ì ê²½ì° íì
b'\n'ì ëë¤; í ì¤í¸ íì¼ì ê²½ì°,open()ì ëí newline ì¸ì를 ì¬ì©íì¬ ì¸ìëë ì¤ ì¢ ê²°ì를 ì íí ì ììµëë¤.
- readlines(hint=-1, /)¶
ì¤í¸ë¦¼ìì ì¤ ë¦¬ì¤í¸ë¥¼ ì½ê³ ë°íí©ëë¤. hintë ì½ì ì¤ ì를 ì ì´íëë¡ ì§ì í ì ììµëë¤: ì§ê¸ê¹ì§ 모ë ì¤ì ì´ í¬ê¸°(ë°ì´í¸/문ì ë¨ì)ê° hint를 ì´ê³¼íë©´ ëë ì¤ì ì½ì§ ììµëë¤.
hint values of
0or less, as well asNone, are treated as no hint.file.readlines()를 í¸ì¶íì§ ìê³for line in file: ...ì ì¬ì©íì¬ íì¼ ê°ì²´ë¥¼ ì´ë¯¸ ì´í°ë ì´í¸ í ì ììì ì ìíììì¤.
- seek(offset, whence=os.SEEK_SET, /)¶
ì¤í¸ë¦¼ ìì¹ë¥¼ whenceê° ê°ë¦¬í¤ë ìì¹ë¥¼ 기ì¤ì¼ë¡ ìëì ì¼ë¡ í´ìëë ì§ì ë ë°ì´í¸ offsetì¼ë¡ ë³ê²½íê³ , ì ì ë ìì¹ë¥¼ ë°íí©ëë¤. whenceì ê°ì ë¤ìê³¼ ê°ìµëë¤:
os.SEEK_SETëë0â ì¤í¸ë¦¼ì ìì(기본ê°); offsetì 0ì´ê±°ë ììì¬ì¼ í©ëë¤os.SEEK_CURëë1â íì¬ ì¤í¸ë¦¼ ìì¹; offsetì ììì¼ ì ììµëë¤os.SEEK_ENDëë2â ì¤í¸ë¦¼ì ë; offsetì ì¼ë°ì ì¼ë¡ ììì ëë¤
Added in version 3.1:
SEEK_*ìì.Added in version 3.3: ì¼ë¶ ì´ì ì²´ì ë
os.SEEK_HOLEì´ëos.SEEK_DATAì ê°ì ì¶ê° ê°ì ì§ìí ì ììµëë¤. íì¼ì ëí´ ì í¨í ê°ì ê·¸ê²ì´ í ì¤í¸ë ë°ì´ë리 모ë ì¤ ì´ë ê²ì¼ë¡ ì´ë ¸ëì§ì ë°ë¼ ë¬ë¼ì§ ì ììµëë¤.
- seekable()¶
ì¤í¸ë¦¼ì´ 무ìì ì¡ì¸ì¤ë¥¼ ì§ìíë©´
True를 ë°íí©ëë¤.Falseì´ë©´,seek(),tell()ë°truncate()ê°OSError를 ë°ììíµëë¤.
- tell()¶
íì¬ì ì¤í¸ë¦¼ ìì¹ë¥¼ ë°íí©ëë¤.
- truncate(size=None, /)¶
ë°ì´í¸ ë¨ìë¡ ì§ì ë sizeë¡ ì¤í¸ë¦¼ í¬ê¸°ë¥¼ ì¡°ì í©ëë¤ (ëë sizeê° ì§ì ëì§ ìì¼ë©´ íì¬ ìì¹). íì¬ ì¤í¸ë¦¼ ìì¹ë ë³ê²½ëì§ ììµëë¤. ì´ í¬ê¸° ì¡°ì ì íì¬ íì¼ í¬ê¸°ë¥¼ ë리거ë ì¤ì¼ ì ììµëë¤. íì¥ì ê²½ì°, ì íì¼ ììì ë´ì©ì íë«í¼ì ë°ë¼ ë¤ë¦ ëë¤ (ëë¶ë¶ì ìì¤í ìì, ì¶ê° ë°ì´í¸ë 0ì¼ë¡ ì±ìì§ëë¤). ì íì¼ í¬ê¸°ê° ë°íë©ëë¤.
ë²ì 3.5ìì ë³ê²½: ìëì°ë ì´ì íì¥ ì íì¼ì 0ì¼ë¡ ì±ìëë¤.
- writable()¶
ì¤í¸ë¦¼ì´ ì°ê¸°ë¥¼ ì§ìíë©´
True를 ë°íí©ëë¤.Falseì´ë©´,write()ìtruncate()ëOSError를 ë°ììíµëë¤.
- writelines(lines, /)¶
ì¤í¸ë¦¼ì ì¤ ë¦¬ì¤í¸ë¥¼ ìëë¤. ì¤ êµ¬ë¶ìë ì¶ê°ëì§ ììì, ì ê³µë ê° ì¤ ëì ì¤ êµ¬ë¶ìê° ìë ê²ì´ ì¼ë°ì ì ëë¤.
- class io.RawIOBase¶
ìì ë°ì´ë리 ì¤í¸ë¦¼ì ë² ì´ì¤ í´ëì¤.
IOBase를 ììí©ëë¤.ìì ë°ì´ë리 ì¤í¸ë¦¼ì ì¼ë°ì ì¼ë¡ íë¶ OS ì¥ì¹ë APIì ëí ì ìì¤ ì¡ì¸ì¤ë¥¼ ì ê³µíë©°, ì´ê²ì ê³ ìì¤ í리미í°ë¸ë¡ 캡ìííë ¤ê³ íì§ ììµëë¤ (ì´ ê¸°ë¥ì ì´ íì´ì§ìì ëì¤ì ì¤ëª í ë²í¼ë§ ë ë°ì´ë리 ì¤í¸ë¦¼ê³¼ í ì¤í¸ ì¤í¸ë¦¼ìì ê³ ìì¤ì¼ë¡ ìíë©ëë¤).
RawIOBaseëIOBaseìì ì¨ ê² ì¸ì ì´ ë©ìë를 ì ê³µí©ëë¤:- read(size=-1, /)¶
ê°ì²´ìì ìµë size ë°ì´í¸ë¥¼ ì½ê³ ë°íí©ëë¤. í¸ìì, sizeê° ì§ì ëì§ ìê±°ë -1ì´ë©´, EOFê¹ì§ì 모ë ë°ì´í¸ê° ë°íë©ëë¤. ê·¸ë ì§ ìì¼ë©´, íëì ìì¤í í¸ì¶ë§ ìíë©ëë¤. ì´ì ì²´ì ìì¤í í¸ì¶ì´ size ë°ì´í¸ 미ë§ì ë°ííë©´ size ë°ì´í¸ 미ë§ì´ ë°íë ì ììµëë¤.
0ë°ì´í¸ê° ë°íëê³ , sizeê° 0ì´ ìëë©´, íì¼ì ëì ëíë ëë¤. ê°ì²´ê° ë¹ ë¸ë¡í¹ 모ëì´ê³ ì¬ì© ê°ë¥í ë°ì´í¸ê° ìì¼ë©´
Noneì´ ë°íë©ëë¤.기본 구íì
readall()ê³¼readinto()ë¡ ììí©ëë¤.
- readall()¶
íìí ê²½ì° ì¤í¸ë¦¼ì ëí ë¤ì¤ í¸ì¶ì ì¬ì©íì¬, EOFê¹ì§ ì¤í¸ë¦¼ì 모ë ë°ì´í¸ë¥¼ ì½ê³ ë°íí©ëë¤.
- readinto(b, /)¶
미리 í ë¹ëê³ , ì°ê¸° ê°ë¥í ë°ì´í¸ì´ë¥ ê°ì²´ bë¡ ë°ì´í¸ë¥¼ ì½ê³ ì½ì ë°ì´í¸ ì를 ë°íí©ëë¤. ì를 ë¤ì´, bë
bytearrayì¼ ì ììµëë¤. ê°ì²´ê° ë¹ ë¸ë¡í¹ 모ëì´ê³ ì¬ì© ê°ë¥í ë°ì´í¸ê° ìì¼ë©´,Noneì´ ë°íë©ëë¤.
- write(b, /)¶
주ì´ì§ ë°ì´í¸ì´ë¥ ê°ì²´, b를 íë¶ ìì ì¤í¸ë¦¼ì ì°ê³ , ì´ ë°ì´í¸ ì를 ë°íí©ëë¤. íë¶ ìì ì¤í¸ë¦¼ì í¹ì±ì ë°ë¼, í¹í ë¹ ë¸ë¡í¹ 모ëì´ë©´ ë°ì´í¸ ë¨ìë¡ bì 길ì´ë³´ë¤ ì§§ì ì ììµëë¤. ìì ì¤í¸ë¦¼ì´ ë¸ë¡ íì§ ìëë¡ ì¤ì ëìê³ ë¨ì¼ ë°ì´í¸ë¥¼ ë¹ì¥ ì¸ ì ìì¼ë©´
Noneì´ ë°íë©ëë¤. í¸ì¶ìë ì´ ë©ìëê° ë°íë í b를 í´ì íê±°ë ë³ê²½í ì ìì´ì, 구íì ë©ìë í¸ì¶ ì¤ìë§ bì ì¡ì¸ì¤í´ì¼ í©ëë¤.
- class io.BufferedIOBase¶
ì´ë¤ ì¢ ë¥ì ë²í¼ë§ì ì§ìíë ë°ì´ë리 ì¤í¸ë¦¼ì ë² ì´ì¤ í´ëì¤.
IOBase를 ììí©ëë¤.The main difference with
RawIOBaseis that methodsread(),readinto()andwrite()will try (respectively) to read as much input as requested or to emit all provided data.In addition, if the underlying raw stream is in non-blocking mode, when the system returns would block
write()will raiseBlockingIOErrorwithBlockingIOError.characters_writtenandread()will return data read so far orNoneif no data is available.ëí,
read()ë©ìëìëreadinto()ë¡ ììíë 기본 구íì´ ììµëë¤.ì¼ë°ì ì¸
BufferedIOBase구íìRawIOBase구íìì ììíì§ ë§ê³ ,BufferedWriterìBufferedReaderì²ë¼ ê°ì¸ì¼ í©ëë¤.BufferedIOBaseëIOBaseìì ì¨ ê² ì¸ì ë¤ì ë°ì´í° ì´í¸ë¦¬ë·°í¸ì ë©ìë를 ì ê³µíê±°ë ì¬ì ìí©ëë¤:- raw¶
BufferedIOBaseê° ë¤ë£¨ë íë¶ ìì ì¤í¸ë¦¼ (RawIOBaseì¸ì¤í´ì¤). ì´ê²ìBufferedIOBaseAPIì ì¼ë¶ê° ìëë©° ì¼ë¶ 구íìë ìì ì ììµëë¤.
- detach()¶
íë¶ ìì ì¤í¸ë¦¼ì ë²í¼ìì ë¶ë¦¬íê³ ë°íí©ëë¤.
ìì ì¤í¸ë¦¼ì´ ë¶ë¦¬ë íìë, ë²í¼ê° ì¬ì©í ì ìë ìíê° ë©ëë¤.
BytesIOì ê°ì ì¼ë¶ ë²í¼ìë ì´ ë©ìëê° ë°íí ë¨ì¼ ìì ì¤í¸ë¦¼ ê°ë ì´ ììµëë¤. ê·¸ë¤ìUnsupportedOperationì ë°ììíµëë¤.Added in version 3.1.
- read(size=-1, /)¶
Read and return up to size bytes. If the argument is omitted,
None, or negative read as much as possible.Fewer bytes may be returned than requested. An empty
bytesobject is returned if the stream is already at EOF. More than one read may be made and calls may be retried if specific errors are encountered, seeos.read()and PEP 475 for more details. Less than size bytes being returned does not imply that EOF is imminent.When reading as much as possible the default implementation will use
raw.readallif available (which should implementRawIOBase.readall()), otherwise will read in a loop until read returnsNone, an emptybytes, or a non-retryable error. For most streams this is to EOF, but for non-blocking streams more data may become available.ì°¸ê³
When the underlying raw stream is non-blocking, implementations may either raise
BlockingIOErroror returnNoneif no data is available.ioimplementations returnNone.
- read1(size=-1, /)¶
Read and return up to size bytes, calling
readinto()which may retry ifEINTRis encountered per PEP 475. If size is-1or not provided, the implementation will choose an arbitrary value for size.ì°¸ê³
When the underlying raw stream is non-blocking, implementations may either raise
BlockingIOErroror returnNoneif no data is available.ioimplementations returnNone.
- readinto(b, /)¶
미리 í ë¹ëê³ , ì°ê¸° ê°ë¥í ë°ì´í¸ì´ë¥ ê°ì²´ bë¡ ë°ì´í¸ë¥¼ ì½ê³ ì½ì ë°ì´í¸ ì를 ë°íí©ëë¤. ì를 ë¤ì´, bë
bytearrayì¼ ì ììµëë¤.read()ì ë§ì°¬ê°ì§ë¡, íë¶ ìì ì¤í¸ë¦¼ì´ ëíìì´ ìë í, ì¬ë¬ ì½ê¸°ê° ìíë ì ììµëë¤.íë¶ ìì ì¤í¸ë¦¼ì´ ë¹ ë¸ë¡í¹ 모ëì´ê³ , íì¬ ì¬ì© ê°ë¥í ë°ì´í°ê° ìì¼ë©´
BlockingIOErrorê° ë°ìí©ëë¤.
- readinto1(b, /)¶
íë¶ ìì ì¤í¸ë¦¼ì
read()(ëëreadinto()) ë©ìë를 ìµë í ë²ë§ í¸ì¶íì¬, 미리 í ë¹ë ì°ê¸° ê°ë¥í ë°ì´í¸ì´ë¥ ê°ì²´ bë¡ ë°ì´í¸ë¥¼ ì½ìµëë¤. ì½ì ë°ì´í¸ ì를 ë°íí©ëë¤.íë¶ ìì ì¤í¸ë¦¼ì´ ë¹ ë¸ë¡í¹ 모ëì´ê³ , íì¬ ì¬ì© ê°ë¥í ë°ì´í°ê° ìì¼ë©´
BlockingIOErrorê° ë°ìí©ëë¤.Added in version 3.5.
- write(b, /)¶
주ì´ì§ ë°ì´í¸ì´ë¥ ê°ì²´, b를 ì°ê³ 기ë¡ë ë°ì´í¸ ì를 ë°íí©ëë¤ (ì°ê¸°ì ì¤í¨íë©´
OSErrorê° ë°ìí기 ë문ì íì ë°ì´í¸ ë¨ìë¡ b 길ì´ì ê°ìµëë¤). ì¤ì 구íì ë°ë¼, ì´ë¬í ë°ì´í¸ë íë¶ ì¤í¸ë¦¼ì ì½ê² ì¸ ì ìê±°ë, ì±ë¥ê³¼ ì§ì° ì´ì ë¡ ë²í¼ì ë³´ê´ë ì ììµëë¤.ë¹ ë¸ë¡í¹ 모ëìì, ë°ì´í°ë¥¼ ìì ì¤í¸ë¦¼ì 기ë¡í´ì¼ íì§ë§ ìì ì¤í¸ë¦¼ì´ ë¸ë¡í¹ ìì´ ëª¨ë ë°ì´í°ë¥¼ ë°ìë¤ì¼ ì ìì¼ë©´,
BlockingIOErrorê° ë°ìí©ëë¤.í¸ì¶ìë ì´ ë©ìëê° ë°íë í b를 í´ì íê±°ë ë³ê²½í ì ìì´ì, 구íì ë©ìë í¸ì¶ ì¤ìë§ bì ì¡ì¸ì¤í´ì¼ í©ëë¤.
ìì íì¼ I/O¶
- class io.FileIO(name, mode='r', closefd=True, opener=None)¶
ë°ì´í¸ì´ ë°ì´í°ë¥¼ í¬í¨íë OS ìì¤ íì¼ì ëíë´ë ìì ë°ì´ë리 ì¤í¸ë¦¼.
RawIOBase를 ììí©ëë¤.nameì ë¤ì ë ê°ì§ ì¤ íëì¼ ì ììµëë¤:
ì´ë¦´ íì¼ì ê²½ë¡ë¥¼ ëíë´ë 문ìì´ì´ë
bytesê°ì²´. ì´ ê²½ì° closefdëTrue(기본ê°)ì´ì´ì¼ í©ëë¤. ê·¸ë ì§ ìì¼ë©´ ìë¬ê° ë°ìí©ëë¤.ê²°ê³¼
FileIOê°ì²´ê° ì¡ì¸ì¤í 기존 OS ìì¤ íì¼ ê¸°ì ìì ë²í¸ë¥¼ ëíë´ë ì ì. FileIO ê°ì²´ê° ë«í ë, closefdê°Falseë¡ ì¤ì ëì´ ìì§ ìì í ì´ fdë ë«íëë¤.
modeë ì½ê¸°(기본ê°), ì°ê¸°, ë°°íì ìì± ëë ë§ë¶ì´ê¸°ë¥¼ ìí´
'r','w','x'ëë'a'ì¼ ì ììµëë¤. ì°ê¸°ë ë§ë¶ì´ê¸°ë¡ ì´ ë íì¼ì´ ì¡´ì¬íì§ ìì¼ë©´ ë§ë¤ì´ì§ëë¤; ì°ê¸° ìí´ ì´ë©´ íì¼ì´ ì립ëë¤. ìì±ì ìí´ ì´ìì ë íì¼ì´ ì´ë¯¸ ì¡´ì¬íë©´FileExistsErrorê° ë°ìí©ëë¤. ìì±í기 ìí´ íì¼ì ì¬ë ê²ì ì°ê¸°ë¥¼ ì미íë¯ë¡, ì´ ëª¨ëë'w'ì ì ì¬í ë°©ìì¼ë¡ ìëí©ëë¤. ì½ê¸°ì ì°ê¸°ë¥¼ ëìì íë½íë ¤ë©´'+'를 모ëì ì¶ê°íììì¤.ì´ í´ëì¤ì
read()(ìì ì¸ìë¡ í¸ì¶ëìì ë),readinto()ë°write()ë©ìëë íëì ìì¤í í¸ì¶ë§ ìíí©ëë¤.ì½ë¬ë¸ì openerë¡ ì ë¬íì¬ ì¬ì©ì ì ì ì¤íë를 ì¬ì©í ì ììµëë¤. ê·¸ë¬ë©´ íì¼ ê°ì²´ì íë¶ íì¼ ê¸°ì ìë (name, flags)ë¡ opener를 í¸ì¶íì¬ ì»ìµëë¤. openerë ì´ë¦° íì¼ ê¸°ì ì를 ë°íí´ì¼ í©ëë¤ (
os.openì openerë¡ ì ë¬íë©´Noneì ì ë¬íë ê²ê³¼ ì ì¬í 기ë¥ì´ ë©ëë¤).ìë¡ ë§ë¤ì´ì§ íì¼ì ìì ë¶ê°ë¥í©ëë¤.
opener ë§¤ê° ë³ì ì¬ì©ì ëí ìë
open()ë´ì¥ í¨ì를 참조íììì¤.ë²ì 3.3ìì ë³ê²½: opener ë§¤ê° ë³ìê° ì¶ê°ëììµëë¤.
'x'모ëê° ì¶ê°ëììµëë¤.ë²ì 3.4ìì ë³ê²½: ì´ì íì¼ì´ ìì ë¶ê°ë¥í©ëë¤.
FileIOëRawIOBaseìIOBaseìì ì¨ ê² ì¸ì ë¤ì ë°ì´í° ì´í¸ë¦¬ë·°í¸ë¥¼ ì ê³µí©ëë¤:- mode¶
ìì±ìì ì ê³µë 모ë.
- name¶
íì¼ ì´ë¦. ìì±ìì ì´ë¦ì´ ì§ì ëì§ ìì¼ë©´ íì¼ì íì¼ ê¸°ì ìì ëë¤.
ë²í¼ë§ ë ì¤í¸ë¦¼Â¶
ë²í¼ë§ ë I/O ì¤í¸ë¦¼ì ìì I/Oë³´ë¤ I/O ì¥ì¹ì ëí ë ê³ ìì¤ì ì¸í°íì´ì¤ë¥¼ ì ê³µí©ëë¤.
- class io.BytesIO(initial_bytes=b'')¶
ì¸ ë©ëª¨ë¦¬ ë°ì´í¸ ë²í¼ë¥¼ ì¬ì©íë ë°ì´ë리 ì¤í¸ë¦¼.
BufferedIOBase를 ììí©ëë¤.close()ë©ìëê° í¸ì¶ë ë ë²í¼ê° í기ë©ëë¤.ì íì ì¸ì initial_bytesë ì´ê¸° ë°ì´í°ë¥¼ í¬í¨íë ë°ì´í¸ì´ë¥ ê°ì²´ì ëë¤.
BytesIOëBufferedIOBaseìIOBaseì ë©ìë ì¸ì ë¤ì ë©ìë를 ì ê³µíê±°ë ì¬ì ìí©ëë¤:- getbuffer()¶
ë³µì¬íì§ ìê³ ë²í¼ì ë´ì©ì ëí ì½ì ì ìê³ ì¸ ì ìë 뷰를 ë°íí©ëë¤. ëí, 뷰를 ë³ê²½íë©´ ë²í¼ì ë´ì©ì´ í¬ëª íê² ê°±ì ë©ëë¤:
>>> b = io.BytesIO(b"abcdef") >>> view = b.getbuffer() >>> view[2:4] = b"56" >>> b.getvalue() b'ab56ef'
ì°¸ê³
ë·°ê° ì¡´ì¬íë í,
BytesIOê°ì²´ì í¬ê¸°ë¥¼ ì¡°ì íê±°ë ë«ì ì ììµëë¤.Added in version 3.2.
- read1(size=-1, /)¶
BytesIOìì, ì´ê²ìread()ì ê°ìµëë¤.ë²ì 3.7ìì ë³ê²½: size ì¸ìë ì´ì ì íì ì ëë¤.
- readinto1(b, /)¶
BytesIOìì, ì´ê²ìreadinto()ì ê°ìµëë¤.Added in version 3.5.
- class io.BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)¶
ì½ì ì ìê³ íìí ì ìë(non seekable)
RawIOBaseìì ë°ì´ë리 ì¤í¸ë¦¼ì ëí ê³ ìì¤ì ì¡ì¸ì¤ë¥¼ ì ê³µíë ë²í¼ë§ ë ë°ì´ë리 ì¤í¸ë¦¼.BufferedIOBase를 ììí©ëë¤.ì´ ê°ì²´ìì ë°ì´í°ë¥¼ ì½ì ë, íë¶ ìì ì¤í¸ë¦¼ìì ë ë§ì ìì ë°ì´í°ê° ìì²ëì´, ë´ë¶ ë²í¼ì ë³´ê´ë ì ììµëë¤. ë²í¼ë§ ë ë°ì´í°ë íì ì½ê¸°ìì ì§ì ë°íë ì ììµëë¤.
ìì±ìë 주ì´ì§ ì½ì ì ìë raw ì¤í¸ë¦¼ê³¼ buffer_sizeì ëí´
BufferedReader를 ë§ëëë¤. buffer_size를 ìëµíë©´,DEFAULT_BUFFER_SIZEê° ì¬ì©ë©ëë¤.BufferedReaderëBufferedIOBaseìIOBaseì ë©ìë ì¸ì ë¤ì ë©ìë를 ì ê³µíê±°ë ì¬ì ìí©ëë¤:- peek(size=0, /)¶
Return bytes from the stream without advancing the position. The number of bytes returned may be less or more than requested. If the underlying raw stream is non-blocking and the operation would block, returns empty bytes.
- read(size=-1, /)¶
In
BufferedReaderthis is the same asio.BufferedIOBase.read()
- read1(size=-1, /)¶
In
BufferedReaderthis is the same asio.BufferedIOBase.read1()ë²ì 3.7ìì ë³ê²½: size ì¸ìë ì´ì ì íì ì ëë¤.
- class io.BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)¶
ì¸ ì ìê³ íìí ì ìë(non seekable)
RawIOBaseìì ë°ì´ë리 ì¤í¸ë¦¼ì ëí ê³ ìì¤ ì¡ì¸ì¤ë¥¼ ì ê³µíë ë²í¼ë§ ë ë°ì´ë리 ì¤í¸ë¦¼.BufferedIOBase를 ììí©ëë¤.ì´ ê°ì²´ì ì¸ ë, ë°ì´í°ë ì¼ë°ì ì¼ë¡ ë´ë¶ ë²í¼ì ë°°ì¹ë©ëë¤. ë²í¼ë ë¤ìê³¼ ê°ì ë¤ìí ì¡°ê±´ìì íë¶
RawIOBaseê°ì²´ì 기ë¡ë©ëë¤:ê³ë¥ ì¤ì¸ 모ë ë°ì´í°ì ë¹í´ ë²í¼ê° ë무 ììì§ ë;
flush()ê° í¸ì¶ë ë;seek()ì´ ìì²ë ë (BufferedRandomê°ì²´ì ê²½ì°);BufferedWriterê°ì²´ê° ë«íê±°ë íê´´ë ë.
ìì±ìë 주ì´ì§ ì°ê¸° ê°ë¥í raw ì¤í¸ë¦¼ì ëí´
BufferedWriter를 ë§ëëë¤. buffer_sizeê° ì ê³µëì§ ìì¼ë©´, 기본ê°ìDEFAULT_BUFFER_SIZEì ëë¤.BufferedWriterëBufferedIOBaseìIOBaseì ë©ìë ì¸ì ë¤ì ë©ìë를 ì ê³µíê±°ë ì¬ì ìí©ëë¤:- flush()¶
ë²í¼ì ìë ë°ì´í¸ë¥¼ ìì ì¤í¸ë¦¼ì¼ë¡ ê°ì ì¶ë ¥í©ëë¤. ìì ì¤í¸ë¦¼ì´ ë¸ë¡ëë©´
BlockingIOError를 ë°ììì¼ì¼ í©ëë¤.
- write(b, /)¶
Write the bytes-like object, b, and return the number of bytes written. When in non-blocking mode, a
BlockingIOErrorwithBlockingIOError.characters_writtenset is raised if the buffer needs to be written out but the raw stream blocks.
- class io.BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)¶
íìí ì ìë (seekable)
RawIOBaseìì ë°ì´ë리 ì¤í¸ë¦¼ì ê³ ìì¤ì ì¡ì¸ì¤ë¥¼ ì ê³µíë ë²í¼ë§ ë ë°ì´ë리 ì¤í¸ë¦¼.BufferedReaderìBufferedWriter를 ììí©ëë¤.ìì±ìë 첫 ë²ì§¸ ì¸ìë¡ ì£¼ì´ì§ íì ê°ë¥í ìì ì¤í¸ë¦¼ì ëí íë 기(reader)ì 기ë¡ê¸°(writer)를 ë§ëëë¤. buffer_size를 ìëµíë©´ 기본ê°ì
DEFAULT_BUFFER_SIZEì ëë¤.BufferedRandomìBufferedReaderëBufferedWriterê° ìí í ì ìë 모ë ìì ì ìíí ì ììµëë¤. ëí,seek()ê³¼tell()ì´ êµ¬íëëë¡ ë³´ì¥ë©ëë¤.
- class io.BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE, /)¶
íëë ì½ì ì ìê³ , ë¤ë¥¸ íëë ì¸ ì ìë, ë ê°ì íìí ì ìë (non seekable)
RawIOBaseìì ë°ì´ë리 ì¤í¸ë¦¼ì ëí ê³ ìì¤ ì¡ì¸ì¤ë¥¼ ì ê³µíë ë²í¼ë§ ë ë°ì´ë리 ì¤í¸ë¦¼.BufferedIOBase를 ììí©ëë¤.readerì writerë ê°ê° ì½ê³ ì¸ ì ìë
RawIOBaseê°ì²´ì ëë¤. buffer_sizeê° ìëµëë©´ 기본ê°ìDEFAULT_BUFFER_SIZEì ëë¤.BufferedRWPairëUnsupportedOperationì ë°ììí¤ëdetach()를 ì ì¸í 모ëBufferedIOBaseì ë©ìë를 구íí©ëë¤.ê²½ê³
BufferedRWPairë íë¶ ìì ì¤í¸ë¦¼ì ëí ë기íë ì¡ì¸ì¤ë¥¼ ìëíì§ ììµëë¤. readerì writerë¡ ê°ì ê°ì²´ë¥¼ ì ë¬í´ìë ì ë©ëë¤; ëìBufferedRandomì ì¬ì©íììì¤.
í ì¤í¸ I/O¶
- class io.TextIOBase¶
í ì¤í¸ ì¤í¸ë¦¼ì ë² ì´ì¤ í´ëì¤. ì´ í´ëì¤ë ì¤í¸ë¦¼ I/O를 ìí 문ìì ì¤ ê¸°ë° ì¸í°íì´ì¤ë¥¼ ì ê³µí©ëë¤.
IOBase를 ììí©ëë¤.TextIOBaseëIOBaseìì ì¨ ê² ì¸ì ë¤ì ë°ì´í° ì´í¸ë¦¬ë·°í¸ì ë©ìë를 ì ê³µíê±°ë ì¬ì ìí©ëë¤:- encoding¶
ì¤í¸ë¦¼ì ë°ì´í¸ì´ì 문ìì´ë¡ ëì½ë©íê³ , 문ìì´ì ë°ì´í¸ì´ë¡ ì¸ì½ë©íë ë° ì¬ì©ëë ì¸ì½ë©ì ì´ë¦.
- errors¶
ëì½ëë ì¸ì½ëì ìë¬ ì¤ì .
- newlines¶
ì§ê¸ê¹ì§ ë²ìë ì¤ ëê¹ì ëíë´ë, 문ìì´, 문ìì´ íí ëë
None. 구íê³¼ ì´ê¸° ìì±ì íëê·¸ì ë°ë¼, ì¬ì©íì§ ëª»í ì ììµëë¤.
- buffer¶
The underlying binary buffer (a
BufferedIOBaseorRawIOBaseinstance) thatTextIOBasedeals with. This is not part of theTextIOBaseAPI and may not exist in some implementations.
- detach()¶
íë¶ ë°ì´ë리 ë²í¼ë¥¼
TextIOBaseì ë¶ë¦¬íì¬ ë°íí©ëë¤.íë¶ ë²í¼ê° ë¶ë¦¬ë íìë,
TextIOBaseë ì¬ì©í ì ìë ìíê° ë©ëë¤.StringIOì ê°ì ì¼ë¶TextIOBase구íìë íë¶ ë²í¼ ê°ë ì´ ìì ì ìì¼ë©° ì´ ë©ìë를 í¸ì¶íë©´UnsupportedOperationì´ ë°ìí©ëë¤.Added in version 3.1.
- read(size=-1, /)¶
ì¤í¸ë¦¼ìì ìµë size 문ì를 ë¨ì¼
strë¡ ì½ê³ ë°íí©ëë¤. sizeê° ììì´ê±°ëNoneì´ë©´ EOFê¹ì§ ì½ìµëë¤.
- readline(size=-1, /)¶
ì¤ ëê¹ì´ë EOFê¹ì§ ì½ê³ ë¨ì¼
strì ë°íí©ëë¤. ì¤í¸ë¦¼ì´ ì´ë¯¸ EOFì ìì¼ë©´, ë¹ ë¬¸ìì´ì´ ë°íë©ëë¤.sizeê° ì§ì ëë©´, ìµë size 문ì를 ì½ìµëë¤.
- seek(offset, whence=SEEK_SET, /)¶
ì¤í¸ë¦¼ ìì¹ë¥¼ ì§ì ë offsetì¼ë¡ ë³ê²½í©ëë¤. ëìì whence ë§¤ê° ë³ìì ë°ë¼ ë¤ë¦ ëë¤. whenceì 기본ê°ì
SEEK_SETì ëë¤.SEEK_SETì´ë0: ì¤í¸ë¦¼ì ììë¶í° íìí©ëë¤ (기본ê°); offsetìTextIOBase.tell()ì´ ë°íí ì«ìì´ê±°ë 0ì´ì´ì¼ í©ëë¤. ë¤ë¥¸ offset ê°ì ì ìëì§ ìì ëìì ìì±í©ëë¤.SEEK_CURì´ë1: íì¬ ìì¹ë¡ âseekâ í©ëë¤; offsetì 0ì´ì´ì¼ íë©°, ì´ë ìë¬´ë° ì¼ë íì§ ììµëë¤ (ë¤ë¥¸ 모ë ê°ì ì§ìëì§ ììµëë¤).SEEK_ENDë2: ì¤í¸ë¦¼ì ëì¼ë¡ seek í©ëë¤; offsetì 0ì´ì´ì¼ í©ëë¤ (ë¤ë¥¸ 모ë ê°ì ì§ìëì§ ììµëë¤).
ìë¡ì´ ì ë ìì¹ë¥¼ ë¶í¬ëª í ì«ìë¡ ë°íí©ëë¤.
Added in version 3.1:
SEEK_*ìì.
- tell()¶
íì¬ ì¤í¸ë¦¼ ìì¹ë¥¼ ë¶í¬ëª í ì«ìë¡ ë°íí©ëë¤. ì«ìë ì¼ë°ì ì¼ë¡ íë¶ ë°ì´ë리 ì ì¥ìì ë°ì´í¸ ì를 ëíë´ì§ ììµëë¤.
- write(s, /)¶
문ìì´ s를 ì¤í¸ë¦¼ì ì°ê³ ì´ ë¬¸ì ì를 ë°íí©ëë¤.
- class io.TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False, write_through=False)¶
BufferedIOBaseë²í¼ë§ ë ë°ì´ë리 ì¤í¸ë¦¼ì ëí ê³ ìì¤ì ì¡ì¸ì¤ë¥¼ ì ê³µíë ë²í¼ë§ ë í ì¤í¸ ì¤í¸ë¦¼.TextIOBase를 ììí©ëë¤.encoding gives the name of the encoding that the stream will be decoded or encoded with. In UTF-8 Mode, this defaults to UTF-8. Otherwise, it defaults to
locale.getencoding().encoding="locale"can be used to specify the current localeâs encoding explicitly. See í ì¤í¸ ì¸ì½ë© for more information.errorsë ì¸ì½ë©ê³¼ ëì½ë© ìë¬ ì²ë¦¬ ë°©ë²ì ì§ì íë ì íì 문ìì´ì ëë¤. ì¸ì½ë© ìë¬ê° ìì ë
ValueErrorìì¸ë¥¼ ë°ììí¤ë ¤ë©´'strict'를 ì ë¬íê³ (기본ê°Noneì ê°ì í¨ê³¼ë¥¼ ì¤ëë¤), ìë¬ë¥¼ 무ìíë ¤ë©´'ignore'를 ì ë¬íììì¤. (ì¸ì½ë© ìë¬ë¥¼ 무ìíë©´ ë°ì´í°ê° ìì¤ë ì ììì ì ìíììì¤.)'replace'ë ì못ë ë°ì´í°ê° ìë ê³³ì ëì²´ ë§ì»¤(ê°ë ¹'?')ê° ì½ì ëëë¡ í©ëë¤.'backslashreplace'ë ì못ë ë°ì´í°ë¥¼ ì ì¬ëì ì´ì¤ì¼ì´í ìíì¤ë¡ ëì²´í©ëë¤. 기ë¡í ë,'xmlcharrefreplace'(ì ì í XML 문ì ì°¸ì¡°ë¡ ëì²´í©ëë¤)ë'namereplace'(\N{...}ì´ì¤ì¼ì´í ìíì¤ë¡ ëì²´í©ëë¤)를 ì¬ì©í ì ììµëë¤.codecs.register_error()ë¡ ë±ë¡ë ë¤ë¥¸ ìë¬ ì²ë¦¬ ì´ë¦ë ì í¨í©ëë¤.newlineì ì¤ ë ì²ë¦¬ ë°©ë²ì ì ì´í©ëë¤.
None,'','\n','\r'ë°'\r\n'ì¼ ì ììµëë¤. ë¤ìê³¼ ê°ì´ ìëí©ëë¤:ì¤í¸ë¦¼ìì ì ë ¥ì ì½ì ë, newlineì´
Noneì´ë©´, ì ëë²ì¤ ì¤ ëê¹ ëª¨ëê° íì±íë©ëë¤. ì ë ¥ì ì¤ì'\n','\r'ëë'\r\n'ì¼ë¡ ëë ì ìì¼ë©°, í¸ì¶ììê² ë°íë기 ì ì'\n'ì¼ë¡ ë³íë©ëë¤. newlineì´''ì´ë©´, ì ëë²ì¤ ì¤ ëê¹ ëª¨ëê° íì±íëì§ë§, ì¤ ëì ë³íëì§ ìì ìíë¡ í¸ì¶ììê² ë°íë©ëë¤. newlineì´ ë¤ë¥¸ ì í¨í ê°ì´ë©´, ì ë ¥ ì¤ì 주ì´ì§ 문ìì´ë¡ë§ ëëê³ , ì¤ ëì ë³íëì§ ìì ìíë¡ í¸ì¶ììê² ë°íë©ëë¤.ì¤í¸ë¦¼ì ì¶ë ¥ì 기ë¡í ë, newlineì´
Noneì´ë©´, 기ë¡ëë 모ë'\n'문ìë ìì¤í 기본 ì¤ êµ¬ë¶ìos.linesepì¼ë¡ ë³íë©ëë¤. newlineì´''ì´ë'\n'ì´ë©´, ë³íì´ ìíëì§ ììµëë¤. newlineì´ ë¤ë¥¸ ì í¨í ê°ì´ë©´, 기ë¡ëë 모ë'\n'문ìë 주ì´ì§ 문ìì´ë¡ ë³íë©ëë¤.
line_bufferingì´
Trueì´ë©´, write í¸ì¶ì ì¤ ëê¹ ë¬¸ìë ìºë¦¬ì§ 리í´ì´ í¬í¨ëì´ ìì¼ë©´flush()ê° ììë©ëë¤.write_throughê°
Trueì´ë©´,write()ì ëí í¸ì¶ì ë²í¼ë§ ëì§ ììì´ ë³´ì¥ë©ëë¤:TextIOWrapperê°ì²´ì 기ë¡ë 모ë ë°ì´í°ë ì¦ì íë¶ ë°ì´ë리 bufferë¡ ì²ë¦¬ë©ëë¤.ë²ì 3.3ìì ë³ê²½: write_through ì¸ìê° ì¶ê°ëììµëë¤.
ë²ì 3.3ìì ë³ê²½: 기본 encodingì ì´ì
locale.getpreferredencoding()ëìlocale.getpreferredencoding(False)ì ëë¤.locale.setlocale()ì ì¬ì©íì¬ ìì ë¡ì¼ì¼ ì¸ì½ë©ì ë³ê²½íì§ ìê³ , ì¬ì©ìê° ì í¸íë ì¸ì½ë© ëì íì¬ ë¡ì¼ì¼ ì¸ì½ë©ì ì¬ì©í©ëë¤.ë²ì 3.10ìì ë³ê²½: The encoding argument now supports the
"locale"dummy encoding name.ì°¸ê³
When the underlying raw stream is non-blocking, a
BlockingIOErrormay be raised if a read operation cannot be completed immediately.TextIOWrapperëTextIOBaseìIOBaseìì ì¨ ê² ì¸ì ë¤ì ë°ì´í° ì´í¸ë¦¬ë·°í¸ì ë©ìë를 ì ê³µí©ëë¤:- line_buffering¶
ì¤ ë²í¼ë§ì´ íì±íëìëì§ ì¬ë¶.
- write_through¶
ì°ê¸°ê° íë¶ ë°ì´ë리 ë²í¼ë¡ ì¦ì ì ë¬ëëì§ ì¬ë¶.
Added in version 3.7.
- reconfigure(*, encoding=None, errors=None, newline=None, line_buffering=None, write_through=None)¶
encoding, errors, newline, line_buffering ë° write_throughì ëí ìë¡ì´ ì¤ì ì ì¬ì©íì¬ ì´ í ì¤í¸ ì¤í¸ë¦¼ì ì¬êµ¬ì±í©ëë¤.
encodingì´ ì§ì ëìì§ë§, errorsê° ì§ì ëì§ ììì ë
errors='strict'ê° ì¬ì©ëë ê²ì ì ì¸íê³ , ì§ì ëì§ ìì ë§¤ê° ë³ìë íì¬ ì¤ì ì ì ì§í©ëë¤.ì¤í¸ë¦¼ìì ì¼ë¶ ë°ì´í°ë¥¼ ì´ë¯¸ ì½ìë¤ë©´ encodingì´ë newlineì ë³ê²½í ì ììµëë¤. ë°ë©´ì, ê¸°ë¡ íì encoding ë³ê²½ì ê°ë¥í©ëë¤.
ì´ ë©ìëë ì ë§¤ê° ë³ì를 ì¤ì í기 ì ì 묵ìì ì¤í¸ë¦¼ íë¬ì를 ìíí©ëë¤.
Added in version 3.7.
ë²ì 3.11ìì ë³ê²½: The method supports
encoding="locale"option.
- seek(cookie, whence=os.SEEK_SET, /)¶
Set the stream position. Return the new stream position as an
int.Four operations are supported, given by the following argument combinations:
seek(0, SEEK_SET): Rewind to the start of the stream.seek(cookie, SEEK_SET): Restore a previous position; cookie must be a number returned bytell().seek(0, SEEK_END): Fast-forward to the end of the stream.seek(0, SEEK_CUR): íì¬ì ì¤í¸ë¦¼ ìì¹ë¥¼ ë³ê²½íì§ ììµëë¤.
Any other argument combinations are invalid, and may raise exceptions.
ë 보기
os.SEEK_SET,os.SEEK_CUR, andos.SEEK_END.
- class io.StringIO(initial_value='', newline='\n')¶
ì¸ ë©ëª¨ë¦¬ í ì¤í¸ ë²í¼ë¥¼ ì¬ì©íë í ì¤í¸ ì¤í¸ë¦¼.
TextIOBase를 ììí©ëë¤.close()ë©ìëê° í¸ì¶ë ë í ì¤í¸ ë²í¼ê° í기ë©ëë¤.ë²í¼ì ì´ê¹ê°ì initial_value를 ì ê³µíì¬ ì¤ì í ì ììµëë¤. ì¤ ë°ê¿ ë³íì´ íì±íëë©´, ì¤ ë°ê¿ì
write()í ê²ì²ë¼ ì¸ì½ë©ë©ëë¤. ì¤í¸ë¦¼ì ë²í¼ì ìì ë¶ë¶ì ìì¹íë©°, ì´ë 기존 íì¼ìw+모ëë¡ ì¬ë ê²ì íë´ë´ì´ ììë¶í° ì¦ì ì°ê¸°ë ì´ê¸° ê°ì í ì´ì°ë ì°ê¸°ì ì¤ë¹ëëë¡ í©ëë¤.a+모ëë¡ íì¼ì ì¬ë ê²ì íë´ë´ë ¤ë©´,f.seek(0, io.SEEK_END)ì ì¬ì©íì¬ ì¤í¸ë¦¼ì ë²í¼ì ëì¼ë¡ ì¬ìì¹ ìí¤ì¸ì.ì¶ë ¥ì ì¤í¸ë¦¼ì ì¸ ë, newlineì´
Noneì´ë©´, 모ë íë«í¼ìì ì¤ ëê¹ì´\në¡ ê¸°ë¡ëë¤ë ì ì ì ì¸íê³ ë, newline ì¸ìëTextIOWrapperìì ì²ë¼ ìëí©ëë¤.StringIOëTextIOBaseìIOBaseì ë©ìë ì¸ì ì´ ë©ìë를 ì ê³µí©ëë¤:- getvalue()¶
ë²í¼ì ì ì²´ ë´ì©ì í¬í¨íë
strì ë°íí©ëë¤. ì¤í¸ë¦¼ ìì¹ë ë³ê²½ëì§ ìì§ë§, ì¤ ëê¹ìread()ì ê°ì´ ëì½ë©ë©ëë¤.
ì¬ì© ì:
import io output = io.StringIO() output.write('First line.\n') print('Second line.', file=output) # íì¼ ë´ì©ì 꺼ë ëë¤ -- 'First line.\nSecond line.\n' ì´ ë©ëë¤. contents = output.getvalue() # ê°ì²´ë¥¼ ë«ê³ ë©ëª¨ë¦¬ ë²í¼ë¥¼ í기í©ëë¤ -- # .getvalue() ë ì´ì ìì¸ë¥¼ ë°ììíµëë¤. output.close()
- class io.IncrementalNewlineDecoder¶
ì ëë²ì¤ ì¤ ëê¹ ëª¨ëì ì¤ ëê¹ ëì½ë©ì íë ëì°ë¯¸ ì½ë±.
codecs.IncrementalDecoder를 ììí©ëë¤.
Static Typing¶
The following protocols can be used for annotating function and method
arguments for simple stream reading or writing operations. They are decorated
with @typing.runtime_checkable.
- class io.Reader[T]¶
Generic protocol for reading from a file or other input stream.
Twill usually bestrorbytes, but can be any type that is read from the stream.Added in version 3.14.
- read()¶
- read(size, /)
Read data from the input stream and return it. If size is specified, it should be an integer, and at most size items (bytes/characters) will be read.
For example:
def read_it(reader: Reader[str]): data = reader.read(11) assert isinstance(data, str)
- class io.Writer[T]¶
Generic protocol for writing to a file or other output stream.
Twill usually bestrorbytes, but can be any type that can be written to the stream.Added in version 3.14.
- write(data, /)¶
Write data to the output stream and return the number of items (bytes/characters) written.
For example:
def write_binary(writer: Writer[bytes]): writer.write(b"Hello world!\n")
See ABCs and Protocols for working with I/O for other I/O related protocols and classes that can be used for static type checking.
ì±ë¥Â¶
ì´ ì¹ì ììë ì ê³µë 구ì I/O 구íì ì±ë¥ì ëí´ ë ¼í©ëë¤.
ë°ì´ë리 I/O¶
ì¬ì©ìê° ë¨ì¼ ë°ì´í¸ë¥¼ ìì²í ëì¡°ì°¨ í° ë°ì´í° ì²í¬ë§ ì½ê³ ì¨ì, ë²í¼ë§ ë I/Oë ì´ì ì²´ì ì ë²í¼ë§ ëì§ ìì I/O 루í´ì í¸ì¶íê³ ì¤íí ëì ë¹í¨ì¨ì±ì ì¨ê¹ëë¤. ì´ëì OS ì ìíëë I/O ì¢ ë¥ì ë°ë¼ ë¤ë¦ ëë¤. ì를 ë¤ì´, 리ë ì¤ì ê°ì ì¼ë¶ ìµì OSììë, ë²í¼ë§ ëì§ ìì ëì¤í¬ I/Oë ë²í¼ë§ ë I/Oë§í¼ ë¹ ë¥¼ ì ììµëë¤. ê·¸ë¬ë ìì ì ë²í¼ë§ ë I/Oê° íë«í¼ê³¼ ë°°ê²½ ì¥ì¹ì ê´ê³ìì´ ì측 ê°ë¥í ì±ë¥ì ì ê³µíë¤ë ê²ì ëë¤. ë°ë¼ì, ë°ì´ë리 ë°ì´í°ìë ë²í¼ë§ ëì§ ìì I/Oë³´ë¤ë ë²í¼ë§ ë I/O를 ì¬ì©íë ê²ì´ ê±°ì íì ë°ëì§í©ëë¤.
í ì¤í¸ I/O¶
ë°ì´ë리 ì ì¥ì(ê°ë ¹ íì¼)ì ëí í
ì¤í¸ I/Oë ê°ì ì ì¥ìì ëí ë°ì´ë리 I/Oë³´ë¤ ìë¹í ë린ë°, 문ì ì½ë±ì ì¬ì©íì¬ ì ëì½ëì ë°ì´ë리 ë°ì´í° ê°ì ë³íì´ íìí기 ë문ì
ëë¤. ì´ê²ì í° ë¡ê·¸ íì¼ê³¼ ê°ì ë§ì ìì í
ì¤í¸ ë°ì´í°ë¥¼ ì²ë¦¬í ë ëë¬ë ì ììµëë¤. ëí, tell()ê³¼ seek()ì ì¬ì©ë ì¬êµ¬ì± ìê³ ë¦¬ì¦ì¼ë¡ ì¸í´ ìë¹í ë립ëë¤.
ê·¸ë¬ë, StringIOë ë¤ì´í°ë¸ ì¸ ë©ëª¨ë¦¬ ì ëì½ë 컨í
ì´ëì´ë©° BytesIOì ë¹ì·í ìë를 ëíë
ëë¤.
ë¤ì¤ ì¤ë ë¶
FileIO ê°ì²´ë ê°ì¼ ì´ì ì²´ì í¸ì¶(ê°ë ¹ ì ëì¤ì read(2))ì´ ì¤ë ë ìì íë¤ë©´ ì¤ë ë ìì í©ëë¤.
ë°ì´ë리 ë²í¼ë§ ë ê°ì²´(BufferedReader, BufferedWriter, BufferedRandom ë° BufferedRWPair ì ì¸ì¤í´ì¤)ë ë¡ì ì¬ì©íì¬ ë´ë¶ 구조를 ë³´í¸í©ëë¤; ë°ë¼ì ì¬ë¬ ì¤ë ëìì ëìì í¸ì¶íë ê²ì´ ìì í©ëë¤.
TextIOWrapper ê°ì²´ë ì¤ë ë ìì íì§ ììµëë¤.
ì¬ì§ì ¶
ë°ì´ë리 ë²í¼ë§ ë ê°ì²´(BufferedReader, BufferedWriter, BufferedRandom ë° BufferedRWPair ì ì¸ì¤í´ì¤)ë ì¬ì§ì
í ì ììµëë¤. ì¬ì§ì
í¸ì¶ì´ ì ìì ì¸ ìí©ììë ë°ìíì§ ìì§ë§, signal ì²ë¦¬ê¸°ìì I/O를 ìííë¤ë©´ ë°ìí ì ììµëë¤. ì¤ë ëê° ì´ë¯¸ ì¡ì¸ì¤ ì¤ì¸ ë²í¼ë§ ë ê°ì²´ì ë¤ì ì§ì
íë ¤ê³ íë©´, RuntimeErrorê° ë°ìí©ëë¤. ì´ê²ì´ ë¤ë¥¸ ì¤ë ëê° ë²í¼ë§ ë ê°ì²´ì ì§ì
íë ê²ì ë§ë ê²ì ìëì ì ìíììì¤.
open() í¨ìë ë²í¼ë§ ë ê°ì²´ë¥¼ TextIOWrapper ìì ê°ì¸ê¸° ë문ì, ìì ë´ì©ì í
ì¤í¸ íì¼ìë 묵ìì ì¼ë¡ íì¥ë©ëë¤. ì¬ê¸°ìë íì¤ ì¤í¸ë¦¼ì´ í¬í¨ëë¯ë¡ ë´ì¥ print() í¨ììë ìí¥ì ì¤ëë¤.