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
397bb248
Commit
397bb248
authored
Jan 12, 2016
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#26001: mention in the tutorial that files in binary mode expect bytes, not str.
parent
f418db2e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
7 deletions
+8
-7
Doc/tutorial/inputoutput.rst
Doc/tutorial/inputoutput.rst
+8
-7
No files found.
Doc/tutorial/inputoutput.rst
View file @
397bb248
...
...
@@ -271,10 +271,11 @@ The rest of the examples in this section will assume that a file object called
``f`` has already been created.
To read a file's contents, call ``f.read(size)``, which reads some quantity of
data and returns it as a string or bytes object. *size* is an optional numeric
argument. When *size* is omitted or negative, the entire contents of the file
will be read and returned; it's your problem if the file is twice as large as
your machine's memory. Otherwise, at most *size* bytes are read and returned.
data and returns it as a string (in text mode) or bytes object (in binary mode).
*size* is an optional numeric argument. When *size* is omitted or negative, the
entire contents of the file will be read and returned; it's your problem if the
file is twice as large as your machine's memory. Otherwise, at most *size* bytes
are read and returned.
If the end of the file has been reached, ``f.read()`` will return an empty
string (``''``). ::
...
...
@@ -315,11 +316,11 @@ the number of characters written. ::
>>> f.write('This is a test\n')
15
To write something other than a string, it needs to be converted to a string
first
::
Other types of objects need to be converted -- either to a string (in text mode)
or a bytes object (in binary mode) -- before writing them
::
>>> value = ('the answer', 42)
>>> s = str(value)
>>> s = str(value)
# convert the tuple to string
>>> f.write(s)
18
...
...
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