@@ -454,7 +454,7 @@ contents, use :func:`shutil.rmtree`.
454454
455455To rename a file, use ``os.rename(old_path, new_path) ``.
456456
457- To truncate a file, open it using ``f = open(filename, "r +") ``, and use
457+ To truncate a file, open it using ``f = open(filename, "rb +") ``, and use
458458``f.truncate(offset) ``; offset defaults to the current seek position. There's
459459also ```os.ftruncate(fd, offset) `` for files opened with :func: `os.open `, where
460460``fd `` is the file descriptor (a small integer).
@@ -483,9 +483,9 @@ in big-endian format from a file::
483483
484484 import struct
485485
486- f = open(filename, "rb") # Open in binary mode for portability
487- s = f.read(8)
488- x, y, z = struct.unpack(">hhl", s)
486+ with open(filename, "rb") as f:
487+ s = f.read(8)
488+ x, y, z = struct.unpack(">hhl", s)
489489
490490The '>' in the format string forces big-endian data; the letter 'h' reads one
491491"short integer" (2 bytes), and 'l' reads one "long integer" (4 bytes) from the
@@ -494,6 +494,13 @@ string.
494494For data that is more regular (e.g. a homogeneous list of ints or thefloats),
495495you can also use the :mod: `array ` module.
496496
497+ .. note ::
498+ To read and write binary data, it is mandatory to open the file in
499+ binary mode (here, passing ``"rb" `` to :func: `open `). If you use
500+ ``"r" `` instead (the default), the file will be open in text mode
501+ and ``f.read() `` will return :class: `str ` objects rather than
502+ :class: `bytes ` objects.
503+
497504
498505I can't seem to use os.read() on a pipe created with os.popen(); why?
499506---------------------------------------------------------------------
@@ -597,27 +604,29 @@ For Unix, see a Usenet post by Mitch Chapman:
597604Why doesn't closing sys.stdout (stdin, stderr) really close it?
598605---------------------------------------------------------------
599606
600- Python file objects are a high-level layer of abstraction on top of C streams,
601- which in turn are a medium-level layer of abstraction on top of (among other
602- things) low-level C file descriptors.
607+ Python :term: `file objects <file object> ` are a high-level layer of
608+ abstraction on low-level C file descriptors.
603609
604- For most file objects you create in Python via the builtin ``file `` constructor,
605- ``f.close() `` marks the Python file object as being closed from Python's point
606- of view, and also arranges to close the underlying C stream. This also happens
607- automatically in f's destructor, when f becomes garbage.
610+ For most file objects you create in Python via the built-in :func: `open `
611+ function, ``f.close() `` marks the Python file object as being closed from
612+ Python's point of view, and also arranges to close the underlying C file
613+ descriptor. This also happens automatically in ``f ``'s destructor, when
614+ ``f `` becomes garbage.
608615
609616But stdin, stdout and stderr are treated specially by Python, because of the
610617special status also given to them by C. Running ``sys.stdout.close() `` marks
611618the Python-level file object as being closed, but does *not * close the
612- associated C stream.
619+ associated C file descriptor.
620+
621+ To close the underlying C file descriptor for one of these three, you should
622+ first be sure that's what you really want to do (e.g., you may confuse
623+ extension modules trying to do I/O). If it is, use :func: `os.close `::
613624
614- To close the underlying C stream for one of these three, you should first be
615- sure that's what you really want to do (e.g., you may confuse extension modules
616- trying to do I/O). If it is, use os.close::
625+ os.close(stdin.fileno())
626+ os.close(stdout.fileno())
627+ os.close(stderr.fileno())
617628
618- os.close(0) # close C's stdin stream
619- os.close(1) # close C's stdout stream
620- os.close(2) # close C's stderr stream
629+ Or you can use the numeric constants 0, 1 and 2, respectively.
621630
622631
623632Network/Internet Programming
0 commit comments