Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76e5538

Browse files
committed
Some more notes about bytes/string filename APIs.
1 parent bcbfa64 commit 76e5538

3 files changed

Lines changed: 34 additions & 22 deletions

File tree

Doc/library/functions.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -710,12 +710,11 @@ are always available. They are listed here in alphabetical order.
710710

711711
Open a file. If the file cannot be opened, :exc:`IOError` is raised.
712712

713-
*file* is either a string or bytes object giving the name (and the
714-
path if the file isn't in the current working directory) of the
715-
file to be opened or an integer file descriptor of the file to be
716-
wrapped. (If a file descriptor is given, it is closed when the
717-
returned I/O object is closed, unless *closefd* is set to
718-
``False``.)
713+
*file* is either a string or bytes object giving the name (and the path if
714+
the file isn't in the current working directory) of the file to be opened or
715+
an integer file descriptor of the file to be wrapped. (If a file descriptor
716+
is given, it is closed when the returned I/O object is closed, unless
717+
*closefd* is set to ``False``.)
719718

720719
*mode* is an optional string that specifies the mode in which the file is
721720
opened. It defaults to ``'r'`` which means open for reading in text mode.

Doc/library/os.path.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ path names. Vice versa, using bytes objects cannot represent all file
1919
names on Windows (in the standard ``mbcs`` encoding), hence Windows
2020
applications should use string objects to access all files.
2121

22+
.. note::
23+
24+
All of these functions accept either only bytes or only string objects as
25+
their parameters. The result is an object of the same type, if a path or
26+
file name is returned.
27+
2228
.. warning::
2329

2430
On Windows, many of these functions do not properly support UNC pathnames.

Doc/library/os.rst

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ interface).
2222
Extensions peculiar to a particular operating system are also available through
2323
the :mod:`os` module, but using them is of course a threat to portability!
2424

25+
.. note::
26+
27+
All functions accepting path or file names accept both bytes and string
28+
objects, and result in an object of the same type, if a path or file name is
29+
returned.
30+
2531
.. note::
2632

2733
If not separately noted, all functions that claim "Availability: Unix" are
@@ -693,15 +699,16 @@ Files and Directories
693699

694700
.. function:: getcwd()
695701

696-
Return a string representing the current working directory.
697-
May raise UnicodeDecodeError if the current directory path fails
698-
to decode in the file system encoding.
699-
Availability: Unix, Windows.
702+
Return a string representing the current working directory. On Unix
703+
platforms, this function may raise :exc:`UnicodeDecodeError` if the name of
704+
the current directory is not decodable in the file system encoding. Use
705+
:func:`getcwdb` if you need the call to never fail. Availability: Unix,
706+
Windows.
700707

701708

702709
.. function:: getcwdb()
703710

704-
Return a bytestring representing the current working directory.
711+
Return a bytestring representing the current working directory.
705712
Availability: Unix, Windows.
706713

707714

@@ -798,15 +805,15 @@ Files and Directories
798805

799806
.. function:: listdir(path)
800807

801-
Return a list containing the names of the entries in the directory. The list is
802-
in arbitrary order. It does not include the special entries ``'.'`` and
803-
``'..'`` even if they are present in the directory. Availability:
804-
Unix, Windows.
808+
Return a list containing the names of the entries in the directory. The list
809+
is in arbitrary order. It does not include the special entries ``.`` and
810+
``..`` even if they are present in the directory. Availability: Unix,
811+
Windows.
805812

806-
If *path* is a Unicode object, the result will be a list of Unicode objects.
807-
If a filename can not be decoded to unicode, it is skipped. If *path* is a
808-
bytes string, the result will be list of bytes objects included files
809-
skipped by the unicode version.
813+
This function can be called with a bytes or string argument. In the bytes
814+
case, all filenames will be listed as returned by the underlying API. In the
815+
string case, filenames will be decoded using the file system encoding, and
816+
skipped if a decoding error occurs.
810817

811818

812819
.. function:: lstat(path)
@@ -920,9 +927,9 @@ Files and Directories
920927
be converted to an absolute pathname using ``os.path.join(os.path.dirname(path),
921928
result)``.
922929

923-
If the *path* is an Unicode object, the result will also be a Unicode object
924-
and may raise an UnicodeDecodeError. If the *path* is a bytes object, the
925-
result will be a bytes object.
930+
If the *path* is a string object, the result will also be a string object,
931+
and the call may raise an UnicodeDecodeError. If the *path* is a bytes
932+
object, the result will be a bytes object.
926933

927934
Availability: Unix.
928935

0 commit comments

Comments
 (0)