@@ -458,7 +458,7 @@ contents, use :func:`shutil.rmtree`.
458458
459459To rename a file, use ``os.rename(old_path, new_path) ``.
460460
461- To truncate a file, open it using ``f = open(filename, "r +") ``, and use
461+ To truncate a file, open it using ``f = open(filename, "rb +") ``, and use
462462``f.truncate(offset) ``; offset defaults to the current seek position. There's
463463also ```os.ftruncate(fd, offset) `` for files opened with :func: `os.open `, where
464464``fd `` is the file descriptor (a small integer).
@@ -487,9 +487,9 @@ in big-endian format from a file::
487487
488488 import struct
489489
490- f = open(filename, "rb") # Open in binary mode for portability
491- s = f.read(8)
492- x, y, z = struct.unpack(">hhl", s)
490+ with open(filename, "rb") as f:
491+ s = f.read(8)
492+ x, y, z = struct.unpack(">hhl", s)
493493
494494The '>' in the format string forces big-endian data; the letter 'h' reads one
495495"short integer" (2 bytes), and 'l' reads one "long integer" (4 bytes) from the
@@ -498,6 +498,13 @@ string.
498498For data that is more regular (e.g. a homogeneous list of ints or thefloats),
499499you can also use the :mod: `array ` module.
500500
501+ .. note ::
502+ To read and write binary data, it is mandatory to open the file in
503+ binary mode (here, passing ``"rb" `` to :func: `open `). If you use
504+ ``"r" `` instead (the default), the file will be open in text mode
505+ and ``f.read() `` will return :class: `str ` objects rather than
506+ :class: `bytes ` objects.
507+
501508
502509I can't seem to use os.read() on a pipe created with os.popen(); why?
503510---------------------------------------------------------------------
@@ -603,28 +610,29 @@ For Unix, see a Usenet post by Mitch Chapman:
603610Why doesn't closing sys.stdout (stdin, stderr) really close it?
604611---------------------------------------------------------------
605612
606- Python file objects are a high-level layer of abstraction on top of C streams,
607- which in turn are a medium-level layer of abstraction on top of (among other
608- things) low-level C file descriptors.
613+ Python :term: `file objects <file object> ` are a high-level layer of
614+ abstraction on low-level C file descriptors.
609615
610- For most file objects you create in Python via the built-in `` open ` `
611- constructor , ``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 stream.
613- This also happens automatically in ``f ``'s destructor, when `` f `` becomes
614- garbage.
616+ For most file objects you create in Python via the built-in :func: ` open `
617+ function , ``f.close() `` marks the Python file object as being closed from
618+ Python's point of view, and also arranges to close the underlying C file
619+ descriptor. This also happens automatically in ``f ``'s destructor, when
620+ `` f `` becomes garbage.
615621
616622But stdin, stdout and stderr are treated specially by Python, because of the
617623special status also given to them by C. Running ``sys.stdout.close() `` marks
618624the Python-level file object as being closed, but does *not * close the
619- associated C stream.
625+ associated C file descriptor.
626+
627+ To close the underlying C file descriptor for one of these three, you should
628+ first be sure that's what you really want to do (e.g., you may confuse
629+ extension modules trying to do I/O). If it is, use :func: `os.close `::
620630
621- To close the underlying C stream for one of these three, you should first be
622- sure that's what you really want to do (e.g., you may confuse extension modules
623- trying to do I/O). If it is, use os.close::
631+ os.close(stdin.fileno())
632+ os.close(stdout.fileno())
633+ os.close(stderr.fileno())
624634
625- os.close(0) # close C's stdin stream
626- os.close(1) # close C's stdout stream
627- os.close(2) # close C's stderr stream
635+ Or you can use the numeric constants 0, 1 and 2, respectively.
628636
629637
630638Network/Internet Programming
0 commit comments