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
e0a7f4f9
Commit
e0a7f4f9
authored
Sep 28, 2000
by
Fred Drake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add truncate() method to StringIO objects.
This closes SourceForge bug #115527.
parent
d391a349
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
0 deletions
+13
-0
Lib/StringIO.py
Lib/StringIO.py
+13
-0
No files found.
Lib/StringIO.py
View file @
e0a7f4f9
...
...
@@ -13,6 +13,7 @@ buf = f.read() # read until EOF
buf = f.read(n) # read up to n bytes
buf = f.readline() # read until end of line ('
\
n
') or EOF
list = f.readlines()# list of f.readline() results until EOF
f.truncate([size]) # truncate file at to at most size (default: current pos)
f.write(buf) # write at current position
f.writelines(list) # for line in list: f.write(line)
f.getvalue() # return whole file's contents as a string
...
...
@@ -28,6 +29,7 @@ Notes:
- There's a simple test set (see end of this file).
"""
import
errno
import
string
class
StringIO
:
...
...
@@ -102,6 +104,17 @@ class StringIO:
break
line
=
self
.
readline
()
return
lines
def
truncate
(
self
,
size
=
None
):
if
self
.
closed
:
raise
ValueError
,
"I/O operation on closed file"
if
size
is
None
:
size
=
self
.
pos
elif
size
<
0
:
raise
IOError
(
errno
.
EINVAL
,
"Negative size not allowed"
)
elif
size
<
self
.
pos
:
self
.
pos
=
size
self
.
buf
=
self
.
getvalue
()[:
size
]
def
write
(
self
,
s
):
if
self
.
closed
:
raise
ValueError
,
"I/O operation on closed file"
...
...
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