Appending and reading a file. Telling the file object position. Setting the file object position. Clearing the internal buffer before closing the file. Getting the integer file descriptor. Checks if file is connected to a tty -like device. Iterates over the file. Truncates the file. Opening and closing a file. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. Recommended Articles. Save my name, email, and website in this browser for the next time I comment.
This site uses Akismet to reduce spam. Learn how your comment data is processed. By Ankit Lathiya Last updated Jan 12, 0. Ankit Lathiya posts 0 comments. You might also like More from author.
Prev Next. It can also allow arbitrary random access seeking forwards or backwards to any location , or only sequential access for example in the case of a socket or pipe. All streams are careful about the type of data you give to them. For example giving a str object to the write method of a binary stream will raise a TypeError. So will giving a bytes object to the write method of a text stream. Changed in version 3. This means that whenever the backing store is natively made of bytes such as in the case of a file , encoding and decoding of data is made transparently as well as optional translation of platform-specific newline characters.
The easiest way to create a text stream is with open , optionally specifying an encoding:. In-memory text streams are also available as StringIO objects:. No encoding, decoding, or newline translation is performed.
This category of streams can be used for all kinds of non-text data, and also when manual control over the handling of text data is desired.
The easiest way to create a binary stream is with open with 'b' in the mode string:. In-memory binary streams are also available as BytesIO objects:. Other library modules may provide additional ways to create text or binary streams.
See socket. Nevertheless, you can create a raw stream by opening a file in binary mode with buffering disabled:. The default encoding of TextIOWrapper and open is locale-specific locale. However, many developers forget to specify the encoding when opening text files encoded in UTF-8 e. This causes bugs because the locale encoding is not UTF-8 for most Windows users. For example:. Additionally, while there is no concrete plan as of yet, Python may change the default text file encoding to UTF-8 in the future.
Accordingly, it is highly recommended that you specify the encoding explicitly when opening text files. When you need to run existing code on Windows that attempts to open UTF-8 files using the default locale encoding, you can enable the UTF-8 mode. See UTF-8 mode on Windows. New in version 3. However, please consider using UTF-8 by default i. This is an alias for the builtin open function.
This function raises an auditing event open with arguments path , mode and flags. The mode and flags arguments may have been modified or inferred from the original call. Opens the provided file with mode 'rb'. This function should be used when the intent is to treat the contents as executable code.
Overriding the behavior is intended for additional validation or preprocessing of the file. This function returns encoding if it is not None and "locale" if encoding is None. This function emits an EncodingWarning if sys. See Text Encoding for more information. This is a compatibility alias for the builtin BlockingIOError exception.
An exception inheriting OSError and ValueError that is raised when an unsupported operation is called on a stream. First abstract base classes ABCs , which are used to specify the various categories of streams, then concrete classes providing the standard stream implementations.
The abstract base classes also provide default implementations of some methods in order to help implementation of concrete stream classes. For example, BufferedIOBase provides unoptimized implementations of readinto and readline. It defines the basic interface to a stream. Note, however, that there is no separation between reading and writing to streams; implementations are allowed to raise UnsupportedOperation if they do not support a given operation.
It deals with the reading and writing of bytes to a stream. It deals with buffering on a raw binary stream RawIOBase. Its subclasses, BufferedWriter , BufferedReader , and BufferedRWPair buffer raw binary streams that are readable, writable, and both readable and writable, respectively. BufferedRandom provides a buffered interface to seekable streams. It deals with streams whose bytes represent text, and handles encoding and decoding to and from strings. Finally, StringIO is an in-memory stream for text.
Argument names are not part of the specification, and only the arguments of open are intended to be used as keyword arguments. The following table summarizes the ABCs provided by the io module:. Inherited IOBase methods, read , and readall. Inherited IOBase methods, readinto , and readinto1.
Inherited IOBase methods, encoding , errors , and newlines. There is no public constructor. This class provides empty abstract implementations for many methods that derived classes can override selectively; the default implementations represent a file that cannot be read, written or seeked.
Even though IOBase does not declare read or write because their signatures will vary, implementations and clients should consider those methods part of the interface. Also, implementations may raise a ValueError or UnsupportedOperation when operations they do not support are called. The basic type used for binary data read from or written to a file is bytes. Other bytes-like objects are accepted as method arguments too.
Note that calling any method even inquiries on a closed stream is undefined. Sorry about that. How can we improve it? Leave this field blank. Python References. Python Library Python print. Python Library Python frozenset. Python Library Python bytes. Python Library Python range. Get App Get Python App.
0コメント