diff options
author | Éric Araujo <merwok@netwok.org> | 2012-11-22 00:14:28 -0500 |
---|---|---|
committer | Éric Araujo <merwok@netwok.org> | 2012-11-22 00:14:28 -0500 |
commit | ded22c6c9d457ec6766e0cdffe3adea1430bcdeb (patch) | |
tree | 5ec9cce034953d91f9d07f489a8e0f7974f004e1 | |
parent | 5a9d7061236991f671beb0ac4d298e9b79ce6f10 (diff) | |
parent | 5bd9270954f5f6bbf46fd32eec1ad9e81aa8eb79 (diff) | |
download | cpython-git-ded22c6c9d457ec6766e0cdffe3adea1430bcdeb.tar.gz |
Merge #13424 followup from 3.3
-rw-r--r-- | Doc/library/functions.rst | 26 |
1 files changed, 4 insertions, 22 deletions
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index f027bac710..a92c378c66 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -937,32 +937,14 @@ are always available. They are listed here in alphabetical order. :mod:`os.open` as *opener* results in functionality similar to passing ``None``). - The following example is an alternative implementation for opening files - for exclusive writing. If we did not have support for the ``'x'`` mode, - we could implement it with this opener:: - - >>> import os - >>> def open_exclusive(path, mode): - ... return os.open(path, mode | os.O_CREAT | os.O_EXCL) - ... - >>> filename = 'spam.txt' - >>> fp = open(filename, 'w', opener=open_exclusive) - >>> fp2 = open(filename, 'w', opener=open_exclusive) - Traceback (most recent call last): - ... - FileExistsError: [Errno 17] File exists: 'spam.txt' - - This other example uses the :ref:`dir_fd` parameter of the + The following example uses the :ref:`dir_fd <dir_fd>` parameter of the :func:`os.open` function to open a file relative to a given directory:: >>> import os - >>> def open_relative(dirname): - ... dir_fd = os.open(dirname, os.O_RDONLY) - ... def opener(path, flags): - ... return os.open(path, flags, dir_fd=dir_fd) - ... return opener, dir_fd + >>> dir_fd = os.open('somedir', os.O_RDONLY) + >>> def opener(path, flags): + ... return os.open(path, flags, dir_fd=dir_fd) ... - >>> opener, dir_fd = open_relative('somedir') >>> with open('spamspam.txt', 'w', opener=opener) as f: ... print('This will be written to somedir/spamspam.txt', file=f) ... |