Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
ded22c6c
Commit
ded22c6c
authored
Nov 22, 2012
by
Éric Araujo
Browse files
Options
Browse Files
Download
Plain Diff
Merge #13424 followup from 3.3
parents
5a9d7061
5bd92709
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
4 additions
and
22 deletions
+4
-22
Doc/library/functions.rst
Doc/library/functions.rst
+4
-22
No files found.
Doc/library/functions.rst
View file @
ded22c6c
...
...
@@ -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)
...
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment