Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
An IO object is a stream: a readable source of bytes or characters or a writable sink for bytes or characters. The File class is a subclass of IO. IO objects also represent the “standard input” and “standard output” streams used to read from and write to the console. The stringio module in the standard library allows us to create a stream wrapper around a string object. Finally, the socket objects used in networking (described later in this chapter) are also IO objects.
Before we can perform input or output, we must have an IO object to read from or write to. The IO class defines factory methods new, open, popen, and pipe, but these are low-level methods with operating system dependencies, and they are not documented here. The subsections that follow describe more common ways to obtain IO objects. (And Section 9.8 includes examples that create IO objects that communicate across the network.)