Walter Dörwald | c2fcf9b | 2003-12-15 10:16:09 +0000 | [diff] [blame] | 1 | r"""File-like objects that read from or write to a string buffer. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 2 | |
| 3 | This implements (nearly) all stdio methods. |
| 4 | |
| 5 | f = StringIO() # ready for writing |
| 6 | f = StringIO(buf) # ready for reading |
| 7 | f.close() # explicitly release resources held |
| 8 | flag = f.isatty() # always false |
| 9 | pos = f.tell() # get current position |
| 10 | f.seek(pos) # set current position |
| 11 | f.seek(pos, mode) # mode 0: absolute; 1: relative; 2: relative to EOF |
| 12 | buf = f.read() # read until EOF |
| 13 | buf = f.read(n) # read up to n bytes |
| 14 | buf = f.readline() # read until end of line ('\n') or EOF |
| 15 | list = f.readlines()# list of f.readline() results until EOF |
Fred Drake | e0a7f4f | 2000-09-28 04:21:06 +0000 | [diff] [blame] | 16 | f.truncate([size]) # truncate file at to at most size (default: current pos) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 17 | f.write(buf) # write at current position |
| 18 | f.writelines(list) # for line in list: f.write(line) |
| 19 | f.getvalue() # return whole file's contents as a string |
| 20 | |
| 21 | Notes: |
| 22 | - Using a real file is often faster (but less convenient). |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 23 | - There's also a much faster implementation in C, called cStringIO, but |
| 24 | it's not subclassable. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 25 | - fileno() is left unimplemented so that code which uses it triggers |
| 26 | an exception early. |
| 27 | - Seeking far beyond EOF and then writing will insert real null |
| 28 | bytes that occupy space in the buffer. |
| 29 | - There's a simple test set (see end of this file). |
| 30 | """ |
Barry Warsaw | c7ed0e3 | 2000-12-12 23:12:23 +0000 | [diff] [blame] | 31 | try: |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 32 | from errno import EINVAL |
Barry Warsaw | c7ed0e3 | 2000-12-12 23:12:23 +0000 | [diff] [blame] | 33 | except ImportError: |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 34 | EINVAL = 22 |
Barry Warsaw | c7ed0e3 | 2000-12-12 23:12:23 +0000 | [diff] [blame] | 35 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 36 | __all__ = ["StringIO"] |
| 37 | |
Martin v. Löwis | 9e62ff2 | 2003-10-18 10:20:42 +0000 | [diff] [blame] | 38 | def _complain_ifclosed(closed): |
| 39 | if closed: |
| 40 | raise ValueError, "I/O operation on closed file" |
| 41 | |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 42 | class StringIO: |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 43 | """class StringIO([buffer]) |
| 44 | |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 45 | When a StringIO object is created, it can be initialized to an existing |
| 46 | string by passing the string to the constructor. If no string is given, |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 47 | the StringIO will start empty. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 48 | |
| 49 | The StringIO object can accept either Unicode or 8-bit strings, but |
| 50 | mixing the two may take some care. If both are used, 8-bit strings that |
| 51 | cannot be interpreted as 7-bit ASCII (that use the 8th bit) will cause |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 52 | a UnicodeError to be raised when getvalue() is called. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 53 | """ |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 54 | def __init__(self, buf = ''): |
Marc-André Lemburg | f853be9 | 2002-01-06 17:15:05 +0000 | [diff] [blame] | 55 | # Force self.buf to be a string or unicode |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 56 | if not isinstance(buf, basestring): |
Marc-André Lemburg | f853be9 | 2002-01-06 17:15:05 +0000 | [diff] [blame] | 57 | buf = str(buf) |
| 58 | self.buf = buf |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 59 | self.len = len(buf) |
| 60 | self.buflist = [] |
| 61 | self.pos = 0 |
Martin v. Löwis | 9e62ff2 | 2003-10-18 10:20:42 +0000 | [diff] [blame] | 62 | self.closed = False |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 63 | self.softspace = 0 |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 64 | |
Barry Warsaw | bdefa0b | 2001-09-22 04:34:54 +0000 | [diff] [blame] | 65 | def __iter__(self): |
Guido van Rossum | c1265bd | 2003-01-31 16:04:15 +0000 | [diff] [blame] | 66 | return self |
| 67 | |
| 68 | def next(self): |
Raymond Hettinger | deb4da5 | 2004-03-14 07:54:37 +0000 | [diff] [blame] | 69 | """A file object is its own iterator, for example iter(f) returns f |
| 70 | (unless f is closed). When a file is used as an iterator, typically |
| 71 | in a for loop (for example, for line in f: print line), the next() |
| 72 | method is called repeatedly. This method returns the next input line, |
| 73 | or raises StopIteration when EOF is hit. |
| 74 | """ |
Guido van Rossum | c1265bd | 2003-01-31 16:04:15 +0000 | [diff] [blame] | 75 | if self.closed: |
| 76 | raise StopIteration |
| 77 | r = self.readline() |
| 78 | if not r: |
| 79 | raise StopIteration |
| 80 | return r |
Barry Warsaw | bdefa0b | 2001-09-22 04:34:54 +0000 | [diff] [blame] | 81 | |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 82 | def close(self): |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 83 | """Free the memory buffer. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 84 | """ |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 85 | if not self.closed: |
Martin v. Löwis | 9e62ff2 | 2003-10-18 10:20:42 +0000 | [diff] [blame] | 86 | self.closed = True |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 87 | del self.buf, self.pos |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 88 | |
| 89 | def isatty(self): |
Raymond Hettinger | deb4da5 | 2004-03-14 07:54:37 +0000 | [diff] [blame] | 90 | """Returns False because StringIO objects are not connected to a |
| 91 | tty-like device. |
| 92 | """ |
Martin v. Löwis | 9e62ff2 | 2003-10-18 10:20:42 +0000 | [diff] [blame] | 93 | _complain_ifclosed(self.closed) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 94 | return False |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 95 | |
| 96 | def seek(self, pos, mode = 0): |
Raymond Hettinger | deb4da5 | 2004-03-14 07:54:37 +0000 | [diff] [blame] | 97 | """Set the file's current position. |
| 98 | |
| 99 | The mode argument is optional and defaults to 0 (absolute file |
| 100 | positioning); other values are 1 (seek relative to the current |
| 101 | position) and 2 (seek relative to the file's end). |
| 102 | |
| 103 | There is no return value. |
| 104 | """ |
Martin v. Löwis | 9e62ff2 | 2003-10-18 10:20:42 +0000 | [diff] [blame] | 105 | _complain_ifclosed(self.closed) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 106 | if self.buflist: |
Marc-André Lemburg | 85d6edf | 2001-02-09 13:37:37 +0000 | [diff] [blame] | 107 | self.buf += ''.join(self.buflist) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 108 | self.buflist = [] |
| 109 | if mode == 1: |
| 110 | pos += self.pos |
| 111 | elif mode == 2: |
| 112 | pos += self.len |
| 113 | self.pos = max(0, pos) |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 114 | |
| 115 | def tell(self): |
Raymond Hettinger | deb4da5 | 2004-03-14 07:54:37 +0000 | [diff] [blame] | 116 | """Return the file's current position.""" |
Martin v. Löwis | 9e62ff2 | 2003-10-18 10:20:42 +0000 | [diff] [blame] | 117 | _complain_ifclosed(self.closed) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 118 | return self.pos |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 119 | |
| 120 | def read(self, n = -1): |
Raymond Hettinger | deb4da5 | 2004-03-14 07:54:37 +0000 | [diff] [blame] | 121 | """Read at most size bytes from the file |
| 122 | (less if the read hits EOF before obtaining size bytes). |
| 123 | |
| 124 | If the size argument is negative or omitted, read all data until EOF |
| 125 | is reached. The bytes are returned as a string object. An empty |
| 126 | string is returned when EOF is encountered immediately. |
| 127 | """ |
Martin v. Löwis | 9e62ff2 | 2003-10-18 10:20:42 +0000 | [diff] [blame] | 128 | _complain_ifclosed(self.closed) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 129 | if self.buflist: |
Marc-André Lemburg | 85d6edf | 2001-02-09 13:37:37 +0000 | [diff] [blame] | 130 | self.buf += ''.join(self.buflist) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 131 | self.buflist = [] |
| 132 | if n < 0: |
| 133 | newpos = self.len |
| 134 | else: |
| 135 | newpos = min(self.pos+n, self.len) |
| 136 | r = self.buf[self.pos:newpos] |
| 137 | self.pos = newpos |
| 138 | return r |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 139 | |
| 140 | def readline(self, length=None): |
Raymond Hettinger | deb4da5 | 2004-03-14 07:54:37 +0000 | [diff] [blame] | 141 | """Read one entire line from the file. |
| 142 | |
| 143 | A trailing newline character is kept in the string (but may be absent |
| 144 | when a file ends with an incomplete line). If the size argument is |
| 145 | present and non-negative, it is a maximum byte count (including the |
| 146 | trailing newline) and an incomplete line may be returned. |
| 147 | |
| 148 | An empty string is returned only when EOF is encountered immediately. |
| 149 | |
| 150 | Note: Unlike stdio's fgets(), the returned string contains null |
| 151 | characters ('\0') if they occurred in the input. |
| 152 | """ |
Martin v. Löwis | 9e62ff2 | 2003-10-18 10:20:42 +0000 | [diff] [blame] | 153 | _complain_ifclosed(self.closed) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 154 | if self.buflist: |
Marc-André Lemburg | 85d6edf | 2001-02-09 13:37:37 +0000 | [diff] [blame] | 155 | self.buf += ''.join(self.buflist) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 156 | self.buflist = [] |
| 157 | i = self.buf.find('\n', self.pos) |
| 158 | if i < 0: |
| 159 | newpos = self.len |
| 160 | else: |
| 161 | newpos = i+1 |
| 162 | if length is not None: |
| 163 | if self.pos + length < newpos: |
| 164 | newpos = self.pos + length |
| 165 | r = self.buf[self.pos:newpos] |
| 166 | self.pos = newpos |
| 167 | return r |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 168 | |
| 169 | def readlines(self, sizehint = 0): |
Raymond Hettinger | deb4da5 | 2004-03-14 07:54:37 +0000 | [diff] [blame] | 170 | """Read until EOF using readline() and return a list containing the |
| 171 | lines thus read. |
| 172 | |
| 173 | If the optional sizehint argument is present, instead of reading up |
| 174 | to EOF, whole lines totalling approximately sizehint bytes (or more |
| 175 | to accommodate a final whole line). |
| 176 | """ |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 177 | total = 0 |
| 178 | lines = [] |
| 179 | line = self.readline() |
| 180 | while line: |
| 181 | lines.append(line) |
| 182 | total += len(line) |
| 183 | if 0 < sizehint <= total: |
| 184 | break |
| 185 | line = self.readline() |
| 186 | return lines |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 187 | |
| 188 | def truncate(self, size=None): |
Raymond Hettinger | deb4da5 | 2004-03-14 07:54:37 +0000 | [diff] [blame] | 189 | """Truncate the file's size. |
| 190 | |
| 191 | If the optional size argument is present, the file is truncated to |
| 192 | (at most) that size. The size defaults to the current position. |
| 193 | The current file position is not changed unless the position |
| 194 | is beyond the new file size. |
| 195 | |
| 196 | If the specified size exceeds the file's current size, the |
| 197 | file remains unchanged. |
| 198 | """ |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 199 | _complain_ifclosed(self.closed) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 200 | if size is None: |
| 201 | size = self.pos |
| 202 | elif size < 0: |
| 203 | raise IOError(EINVAL, "Negative size not allowed") |
| 204 | elif size < self.pos: |
| 205 | self.pos = size |
| 206 | self.buf = self.getvalue()[:size] |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 207 | |
| 208 | def write(self, s): |
Raymond Hettinger | deb4da5 | 2004-03-14 07:54:37 +0000 | [diff] [blame] | 209 | """Write a string to the file. |
| 210 | |
| 211 | There is no return value. |
| 212 | """ |
Martin v. Löwis | 9e62ff2 | 2003-10-18 10:20:42 +0000 | [diff] [blame] | 213 | _complain_ifclosed(self.closed) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 214 | if not s: return |
Marc-André Lemburg | f853be9 | 2002-01-06 17:15:05 +0000 | [diff] [blame] | 215 | # Force s to be a string or unicode |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 216 | if not isinstance(s, basestring): |
Marc-André Lemburg | f853be9 | 2002-01-06 17:15:05 +0000 | [diff] [blame] | 217 | s = str(s) |
Raymond Hettinger | 0336e1f | 2004-09-23 06:43:25 +0000 | [diff] [blame] | 218 | spos = self.pos |
| 219 | slen = self.len |
| 220 | if spos == slen: |
Fred Drake | d679e09 | 2002-09-17 18:10:34 +0000 | [diff] [blame] | 221 | self.buflist.append(s) |
Raymond Hettinger | 513c8bd | 2004-09-23 07:00:47 +0000 | [diff] [blame] | 222 | self.len = self.pos = spos + len(s) |
Fred Drake | d679e09 | 2002-09-17 18:10:34 +0000 | [diff] [blame] | 223 | return |
Raymond Hettinger | 0336e1f | 2004-09-23 06:43:25 +0000 | [diff] [blame] | 224 | if spos > slen: |
| 225 | self.buflist.append('\0'*(spos - slen)) |
| 226 | slen = spos |
| 227 | newpos = spos + len(s) |
| 228 | if spos < slen: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 229 | if self.buflist: |
Marc-André Lemburg | 85d6edf | 2001-02-09 13:37:37 +0000 | [diff] [blame] | 230 | self.buf += ''.join(self.buflist) |
Raymond Hettinger | 0336e1f | 2004-09-23 06:43:25 +0000 | [diff] [blame] | 231 | self.buflist = [self.buf[:spos], s, self.buf[newpos:]] |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 232 | self.buf = '' |
Raymond Hettinger | 0336e1f | 2004-09-23 06:43:25 +0000 | [diff] [blame] | 233 | if newpos > slen: |
Raymond Hettinger | 513c8bd | 2004-09-23 07:00:47 +0000 | [diff] [blame] | 234 | slen = newpos |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 235 | else: |
| 236 | self.buflist.append(s) |
Raymond Hettinger | 513c8bd | 2004-09-23 07:00:47 +0000 | [diff] [blame] | 237 | slen = newpos |
| 238 | self.len = slen |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 239 | self.pos = newpos |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 240 | |
Raymond Hettinger | 6ec0996 | 2004-03-08 18:17:31 +0000 | [diff] [blame] | 241 | def writelines(self, iterable): |
Raymond Hettinger | deb4da5 | 2004-03-14 07:54:37 +0000 | [diff] [blame] | 242 | """Write a sequence of strings to the file. The sequence can be any |
| 243 | iterable object producing strings, typically a list of strings. There |
| 244 | is no return value. |
| 245 | |
| 246 | (The name is intended to match readlines(); writelines() does not add |
| 247 | line separators.) |
| 248 | """ |
Raymond Hettinger | 6ec0996 | 2004-03-08 18:17:31 +0000 | [diff] [blame] | 249 | write = self.write |
| 250 | for line in iterable: |
| 251 | write(line) |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 252 | |
| 253 | def flush(self): |
Raymond Hettinger | deb4da5 | 2004-03-14 07:54:37 +0000 | [diff] [blame] | 254 | """Flush the internal buffer |
| 255 | """ |
Martin v. Löwis | 9e62ff2 | 2003-10-18 10:20:42 +0000 | [diff] [blame] | 256 | _complain_ifclosed(self.closed) |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 257 | |
| 258 | def getvalue(self): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 259 | """ |
| 260 | Retrieve the entire contents of the "file" at any time before |
| 261 | the StringIO object's close() method is called. |
| 262 | |
| 263 | The StringIO object can accept either Unicode or 8-bit strings, |
| 264 | but mixing the two may take some care. If both are used, 8-bit |
| 265 | strings that cannot be interpreted as 7-bit ASCII (that use the |
| 266 | 8th bit) will cause a UnicodeError to be raised when getvalue() |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 267 | is called. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 268 | """ |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 269 | if self.buflist: |
Marc-André Lemburg | 85d6edf | 2001-02-09 13:37:37 +0000 | [diff] [blame] | 270 | self.buf += ''.join(self.buflist) |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 271 | self.buflist = [] |
| 272 | return self.buf |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 273 | |
| 274 | |
| 275 | # A little test suite |
| 276 | |
| 277 | def test(): |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 278 | import sys |
| 279 | if sys.argv[1:]: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 280 | file = sys.argv[1] |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 281 | else: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 282 | file = '/etc/passwd' |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 283 | lines = open(file, 'r').readlines() |
| 284 | text = open(file, 'r').read() |
| 285 | f = StringIO() |
| 286 | for line in lines[:-2]: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 287 | f.write(line) |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 288 | f.writelines(lines[-2:]) |
| 289 | if f.getvalue() != text: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 290 | raise RuntimeError, 'write failed' |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 291 | length = f.tell() |
| 292 | print 'File length =', length |
| 293 | f.seek(len(lines[0])) |
| 294 | f.write(lines[1]) |
| 295 | f.seek(0) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 296 | print 'First line =', repr(f.readline()) |
Neal Norwitz | 9fb289d | 2002-02-11 17:52:18 +0000 | [diff] [blame] | 297 | print 'Position =', f.tell() |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 298 | line = f.readline() |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 299 | print 'Second line =', repr(line) |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 300 | f.seek(-len(line), 1) |
| 301 | line2 = f.read(len(line)) |
| 302 | if line != line2: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 303 | raise RuntimeError, 'bad result after seek back' |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 304 | f.seek(len(line2), 1) |
| 305 | list = f.readlines() |
| 306 | line = list[-1] |
| 307 | f.seek(f.tell() - len(line)) |
| 308 | line2 = f.read() |
| 309 | if line != line2: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 310 | raise RuntimeError, 'bad result after seek back from EOF' |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 311 | print 'Read', len(list), 'more lines' |
| 312 | print 'File length =', f.tell() |
| 313 | if f.tell() != length: |
Fred Drake | a63bd1c | 2000-12-13 20:23:11 +0000 | [diff] [blame] | 314 | raise RuntimeError, 'bad length' |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 315 | f.close() |
Guido van Rossum | 85d8945 | 1994-06-23 11:53:27 +0000 | [diff] [blame] | 316 | |
| 317 | if __name__ == '__main__': |
Barry Warsaw | c140131 | 2000-12-12 23:16:51 +0000 | [diff] [blame] | 318 | test() |