Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
dd21912c
Kaydet (Commit)
dd21912c
authored
Nis 11, 2008
tarafından
Benjamin Peterson
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Synced builtin open and io.open documentation, taking the best of each
üst
d12fbe9e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
121 additions
and
102 deletions
+121
-102
functions.rst
Doc/library/functions.rst
+71
-76
io.rst
Doc/library/io.rst
+50
-26
No files found.
Doc/library/functions.rst
Dosyayı görüntüle @
dd21912c
...
@@ -698,94 +698,89 @@ available. They are listed here in alphabetical order.
...
@@ -698,94 +698,89 @@ available. They are listed here in alphabetical order.
:meth:`__index__` method that returns an integer.
:meth:`__index__` method that returns an integer.
.. function:: open(file
name
[, mode='r'[, buffering=None[, encoding=None[, errors=None[, newline=None[, closefd=True]]]]]])
.. function:: open(file[, mode='r'[, buffering=None[, encoding=None[, errors=None[, newline=None[, closefd=True]]]]]])
Open a file, returning an object of the :class:`file` type described in
Open a file. If the file cannot be opened, :exc:`IOError` is raised.
section :ref:`bltin-file-objects`. If the file cannot be opened,
:exc:`IOError` is raised. When opening a file, it's preferable to use
:func:`open` instead of invoking the :class:`file` constructor directly.
*file
name* is either a string giving the name (and the path if the
*file
* is either a string giving the name (and the path if the file isn't in
file isn't in the current working directory) of the file to b
e
the current working directory) of the file to be opened or an integer fil
e
opened; or an integer file descriptor of the file to be wrapped. (If
descriptor of the file to be wrapped. (If a file descriptor is given, it is
a file descriptor is given, it is closed when the returned I/O object
closed when the returned I/O object is closed, unless *closefd* is set to
is closed, unless *closefd* is set to
``False``.)
``False``.)
*mode* is an optional string that specifies the mode in which the file is
*mode* is an optional string that specifies the mode in which the file is
opened. It defaults to ``'r'`` which means open for reading in text mode.
opened. It defaults to ``'r'`` which means open for reading in text mode.
Other common values are ``'w'`` for writing (truncating the file if
Other common values are ``'w'`` for writing (truncating the file if it
it already exists), and ``'a'`` for appending (which on *some* Unix
already exists), and ``'a'`` for appending (which on *some* Unix systems,
systems means that *all* writes append to the end of the file
means that *all* writes append to the end of the file regardless of the
regardless of the current seek position). In text mode, if *encoding*
current seek position). In text mode, if *encoding* is not specified the
is not specified the encoding used is platform dependent. (For reading
encoding used is platform dependent. (For reading and writing raw bytes use
and writing raw bytes use binary mode and leave *encoding*
binary mode and leave *encoding* unspecified.) The available modes are:
unspecified.) The available modes are:
========= ===============================================================
* 'r' open for reading (default)
Character Meaning
* 'w' open for writing, truncating the file first
--------- ---------------------------------------------------------------
* 'a' open for writing, appending to the end if the file exists
``'r'`` open for reading (default)
* 'b' binary mode
``'w'`` open for writing, truncating the file first
* 't' text mode (default)
``'a'`` open for writing, appending to the end of the file if it exists
* '+' open the file for updating (implies both reading and writing)
``'b'`` binary mode
* 'U' universal newline mode (for backwards compatibility;
``'t'`` text mode (default)
unnecessary in new code)
``'+'`` open a disk file for updating (reading and writing)
``'U'`` universal newline mode (for backwards compatibility; unneeded
The most commonly-used values of *mode* are ``'r'`` for reading, ``'w'`` for
for new code)
writing (truncating the file if it already exists), and ``'a'`` for appending
========= ===============================================================
(which on *some* Unix systems means that *all* writes append to the end of the
file regardless of the current seek position). If *mode* is omitted, it
The default mode is ``'rt'`` (open for reading text). For binary random
defaults to ``'r'``. The default is to use text mode, which may convert
access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while
``'
\n
'`` characters to a platform-specific representation on writing and back
``'r+b'`` opens the file without truncation.
on reading. Thus, when opening a binary file, you should append ``'b'`` to
the *mode* value to open the file in binary mode, which will improve
portability. (Appending ``'b'`` is useful even on systems that don't treat
binary and text files differently, where it serves as documentation.) See below
for more possible values of *mode*.
Python distinguishes between files opened in binary and text modes, even
Python distinguishes between files opened in binary and text modes, even
when the underlying operating system doesn't. Files opened in binary
when the underlying operating system doesn't. Files opened in binary
mode (appending ``'b'`` to the *mode* argument) return contents as
mode (appending ``'b'`` to the *mode* argument) return contents as
``bytes`` objects without any decoding. In text mode (the default,
``bytes`` objects without any decoding. In text mode (the default,
or when
or when
``'t'`` is appended to the *mode* argument) the contents of
``'t'`` is appended to the *mode* argument) the contents of
the file are returned as strings, the bytes having been first decoded
the file are returned as strings, the bytes having been first decoded
using a platform-dependent encoding or using the specified *encoding*
using a platform-dependent encoding or using the specified *encoding*
if given.
if given.
*buffering* is an optional integer used to set the buffering policy. By
*buffering* is an optional integer used to set the buffering policy. By
default full buffering is on. Pass 0 to switch buffering off (only
default full buffering is on. Pass 0 to switch buffering off (only allowed in
allowed in binary mode), 1 to set line buffering, and an integer > 1
binary mode), 1 to set line buffering, and an integer > 1 for full buffering.
for full buffering.
*encoding* is an optional string that specifies the file's encoding when
*encoding* is the name of the encoding used to decode or encode the file.
reading or writing in text mode---this argument should not be used in
This should only be used in text mode. The default encoding is platform
binary mode. The default encoding is platform dependent, but any encoding
dependent, but any encoding supported by Python can be passed. See the
supported by Python can be used. (See the :mod:`codecs` module for
:mod:`codecs` module for the list of supported encodings.
the list of supported encodings.)
*errors* is an optional string that specifies how encoding errors are to be
*errors* is an optional string that specifies how encoding errors are to be
handled---this argument should not be used in binary mode. Pass
handled---this argument should not be used in binary mode. Pass ``'strict'``
``'strict'`` to raise a :exc:`ValueError` exception if there is an encoding
to raise a :exc:`ValueError` exception if there is an encoding error (the
error (the default of ``None`` has the same effect), or pass ``'ignore'``
default of ``None`` has the same effect), or pass ``'ignore'`` to ignore
to ignore errors. (Note that ignoring encoding errors can lead to
errors. (Note that ignoring encoding errors can lead to data loss.) See the
data loss.) See the documentation for :func:`codecs.register` for a
documentation for :func:`codecs.register` for a list of the permitted
list of the permitted encoding error strings.
encoding error strings.
*newline* is an optional string that specifies the newline character(s).
*newline* controls how universal newlines works (it only applies to text
When reading, if *newline* is ``None``, universal newlines mode is enabled.
mode). It can be ``None``, ``''``, ``'
\n
'``, ``'
\r
'``, and ``'
\r\n
'``. It
Lines read in univeral newlines mode can end in ``'
\n
'``, ``'
\r
'``,
works as follows:
or ``'
\r\n
'``, and these are translated into ``'
\n
'``. If *newline*
is ``''``, universal newline mode is enabled, but line endings are
* On input, if *newline* is ``None``, universal newlines mode is enabled.
not translated. If any other string is given, lines are assumed to be
Lines in the input can end in ``'
\n
'``, ``'
\r
'``, or ``'
\r\n
'``, and these
terminated by that string, and no translating is done. When writing,
are translated into ``'
\n
'`` before being returned to the caller. If it is
if *newline* is ``None``, any ``'
\n
'`` characters written are
``''``, universal newline mode is enabled, but line endings are returned to
translated to the system default line separator, :attr:`os.linesep`.
the caller untranslated. If it has any of the other legal values, input
If *newline* is ``''``, no translation takes place. If *newline* is
lines are only terminated by the given string, and the line ending is
any of the other standard values, any ``'
\n
'`` characters written are
returned to the caller untranslated.
translated to the given string.
* On output, if *newline* is ``None``, any ``'
\n
'`` characters written are
*closefd* is an optional Boolean which specifies whether to keep the
translated to the system default line separator, :data:`os.linesep`. If
underlying file descriptor open. It must be ``True`` (the default) if
*newline* is ``''``, no translation takes place. If *newline* is any of
a filename is given.
the other legal values, any ``'
\n
'`` characters written are translated to
the given string.
If *closefd* is ``False``, the underlying file descriptor will be kept open
when the file is closed. This does not work when a file name is given and
must be ``True`` in that case.
.. index::
.. index::
single: line-buffered I/O
single: line-buffered I/O
...
@@ -796,9 +791,9 @@ available. They are listed here in alphabetical order.
...
@@ -796,9 +791,9 @@ available. They are listed here in alphabetical order.
single: text mode
single: text mode
module: sys
module: sys
See also the file handling modules, such as,
See also the file handling modules, such as,
:mod:`fileinput`, :mod:`io`
:mod:`fileinput`, :mod:`os`, :mod:`os.path`, :mod:`tempfile`, and
(where :func:`open()` is declared), :mod:`os`, :mod:`os.path`,
:mod:`shutil`.
:mod:`
tempfile`, and :mod:`
shutil`.
.. XXX works for bytes too, but should it?
.. XXX works for bytes too, but should it?
...
...
Doc/library/io.rst
Dosyayı görüntüle @
dd21912c
...
@@ -44,13 +44,23 @@ Module Interface
...
@@ -44,13 +44,23 @@ Module Interface
.. function:: open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])
.. function:: open(file[, mode[, buffering[, encoding[, errors[, newline[, closefd=True]]]]]])
Open *file* and return a stream.
Open *file* and return a stream. If the file cannot be opened, an
:exc:`IOError` is raised.
*file* is a string giving the name of the file, or an integer file descriptor
of the file to be wrapped.
*file* is either a string giving the name (and the path if the file isn't in
the current working directory) of the file to be opened or an integer file
The optional *mode* string determines how the file is opened and consists of
descriptor of the file to be wrapped. (If a file descriptor is given, it is
a combination of the following characters:
closed when the returned I/O object is closed, unless *closefd* is set to
``False``.)
*mode* is an optional string that specifies the mode in which the file is
opened. It defaults to ``'r'`` which means open for reading in text mode.
Other common values are ``'w'`` for writing (truncating the file if it
already exists), and ``'a'`` for appending (which on *some* Unix systems,
means that *all* writes append to the end of the file regardless of the
current seek position). In text mode, if *encoding* is not specified the
encoding used is platform dependent. (For reading and writing raw bytes use
binary mode and leave *encoding* unspecified.) The available modes are:
========= ===============================================================
========= ===============================================================
Character Meaning
Character Meaning
...
@@ -69,18 +79,31 @@ Module Interface
...
@@ -69,18 +79,31 @@ Module Interface
access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while
access, the mode ``'w+b'`` opens and truncates the file to 0 bytes, while
``'r+b'`` opens the file without truncation.
``'r+b'`` opens the file without truncation.
*buffering* is an optional argument controling the buffering of the returned
Python distinguishes between files opened in binary and text modes, even
stream. A value of ``0`` means no buffering, ``1`` means line buffered, and
when the underlying operating system doesn't. Files opened in binary
a greater value means full buffering with the given buffer size. Buffering
mode (appending ``'b'`` to the *mode* argument) return contents as
cannot be disabled in text mode.
``bytes`` objects without any decoding. In text mode (the default, or when
``'t'`` is appended to the *mode* argument) the contents of
the file are returned as strings, the bytes having been first decoded
using a platform-dependent encoding or using the specified *encoding*
if given.
*
encoding* is the name of the encoding used to decode or encode the file.
*
buffering* is an optional integer used to set the buffering policy. By
This may only be used in text mode. Any encoding available in the
default full buffering is on. Pass 0 to switch buffering off (only allowed in
:mod:`codecs` module registry can be used
.
binary mode), 1 to set line buffering, and an integer > 1 for full buffering
.
*errors* specifies how the encoding should treat errors. "strict", the
*encoding* is the name of the encoding used to decode or encode the file.
default raises a :exc:`ValueError` on problems. See the *errors* argument
This should only be used in text mode. The default encoding is platform
of :func:`codecs.open` for more information. XXX
dependent, but any encoding supported by Python can be passed. See the
:mod:`codecs` module for the list of supported encodings.
*errors* is an optional string that specifies how encoding errors are to be
handled---this argument should not be used in binary mode. Pass ``'strict'``
to raise a :exc:`ValueError` exception if there is an encoding error (the
default of ``None`` has the same effect), or pass ``'ignore'`` to ignore
errors. (Note that ignoring encoding errors can lead to data loss.) See the
documentation for :func:`codecs.register` for a list of the permitted
encoding error strings.
*newline* controls how universal newlines works (it only applies to text
*newline* controls how universal newlines works (it only applies to text
mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It
mode). It can be ``None``, ``''``, ``'\n'``, ``'\r'``, and ``'\r\n'``. It
...
@@ -100,13 +123,14 @@ Module Interface
...
@@ -100,13 +123,14 @@ Module Interface
the other legal values, any ``'\n'`` characters written are translated to
the other legal values, any ``'\n'`` characters written are translated to
the given string.
the given string.
If *closefd* is :keyword:`False`, the underlying file descriptor will be kept
If *closefd* is ``False``, the underlying file descriptor will be kept open
open when the file is closed. This does not work when a file name is given.
when the file is closed. This does not work when a file name is given and
must be ``True`` in that case.
The :func:`open` function returns a file object whose type depends on the
:func:`open()` returns a file object whose type depends on the mode, and
mode, and through which the standard file operations such as reading and
through which the standard file operations such as reading and writing are
writing are performed. When :func:`open` is used to open a file in a text
performed. When :func:`open()` is used to open a file in a text mode
mode
(``'w'``, ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a
(``'w'``, ``'r'``, ``'wt'``, ``'rt'``, etc.), it returns a
:class:`TextIOWrapper`. When used to open a file in a binary mode, the
:class:`TextIOWrapper`. When used to open a file in a binary mode, the
returned class varies: in read binary mode, it returns a
returned class varies: in read binary mode, it returns a
:class:`BufferedReader`; in write binary and append binary modes, it returns
:class:`BufferedReader`; in write binary and append binary modes, it returns
...
@@ -114,9 +138,9 @@ Module Interface
...
@@ -114,9 +138,9 @@ Module Interface
:class:`BufferedRandom`.
:class:`BufferedRandom`.
It is also possible to use a string or bytearray as a file for both reading
It is also possible to use a string or bytearray as a file for both reading
and writing. For strings :class:`
io.StringIO` can be used like a file opened
and writing. For strings :class:`
StringIO` can be used like a file opened in
in a text mode, and for bytes a :class:`io.BytesIO` can be used like a file
a text mode, and for bytes a :class:`BytesIO` can be used like a file opened
opened
in a binary mode.
in a binary mode.
.. exception:: BlockingIOError
.. exception:: BlockingIOError
...
...
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