示例#1
0
    def readline(self, length=None):
        r"""Read one entire line from the file.

        A trailing newline character is kept in the string (but may be absent
        when a file ends with an incomplete line). If the size argument is
        present and non-negative, it is a maximum byte count (including the
        trailing newline) and an incomplete line may be returned.

        An empty string is returned only when EOF is encountered immediately.

        Note: Unlike stdio's fgets(), the returned string contains null
        characters ('\0') if they occurred in the input.
        """
        _complain_ifclosed(self.closed)
        if self.buflist:
            self.buf += ''.join(self.buflist)
            self.buflist = []
        i = self.buf.find('\n', self.pos)
        if i < 0:
            return None # rewrite the code of StringIO
        else:
            newpos = i+1
        if length is not None and length >= 0:
            if self.pos + length < newpos:
                newpos = self.pos + length
        r = self.buf[self.pos:newpos]
        self.pos = newpos
        return r
示例#2
0
    def writelines(self, iterable):
        """
        writelines(iterable) -> None.  Write the iterable of strings to file.

        Note that newlines are not added.  This is equalivalent to calling
        write() for each string in the iterable.
        """
        _complain_ifclosed(self.closed)
        _complain_ifreadonly(self._isreadonly())
        return self._file.writelines(iterable)
示例#3
0
    def write(self, str):
        """
        write(str) -> None.  Write string str to file.

        Note that due to buffering, flush() or close() are needed before
        the database reflects the data written.
        """
        _complain_ifclosed(self.closed)
        _complain_ifreadonly(self._isreadonly())
        return self._file.write(str)
示例#4
0
    def seek(self, offset, whence=0):
        """
        seek(offset[, whence]) -> None.  Move to new file position.

        Argument offset is a byte count.  Optional argument whence defaults to
        0 (offset from start of file, offset should be >= 0); other values are 1
        (move relative to current position, positive or negative), and 2 (move
        relative to end of file, usually negative, but allows seeking beyond
        the end of a file).
        """
        _complain_ifclosed(self.closed)
        return self._file.seek(offset, whence)
示例#5
0
    def truncate(self, size=None):
        """
        truncate([size]) -> None.  Truncate the file to at most size bytes.

        Size defaults to the current file position, as returned by tell().
        """
        _complain_ifclosed(self.closed)
        _complain_ifreadonly(self._isreadonly())
        position = self._file.tell()
        try:
            if size is not None:
                self._file.seek(size)
            return self._file.truncate()
        finally:
            self._file.seek(position)
示例#6
0
 def isatty(self):
     """isatty() -> False."""
     _complain_ifclosed(self.closed)
     return False
示例#7
0
 def next(self):
     _complain_ifclosed(self.closed)
     r = self._file.readline()
     if not r:
         raise StopIteration
     return r
示例#8
0
 def flush(self):
     """flush() -> None.  Flush the internal I/O buffer."""
     _complain_ifclosed(self.closed)
     _complain_ifreadonly(self._isreadonly())
     self.save()
示例#9
0
 def readlines(self):
     """readlines() -> list of strings, each a line from the file."""
     _complain_ifclosed(self.closed)
     return self._file.readlines()
示例#10
0
 def readline(self):
     """readline() -> next line from the file, as a string."""
     _complain_ifclosed(self.closed)
     return self._file.readline()
示例#11
0
 def read(self, size=-1):
     """read([size]) -> read at most size bytes, returned as a string."""
     _complain_ifclosed(self.closed)
     return self._file.read(size)
示例#12
0
 def tell(self):
     """
     tell() -> current file position, an integer (may be a long integer).
     """
     _complain_ifclosed(self.closed)
     return self._file.tell()