Python os.SEEK_SETs 代码实例

来源:互联网 发布:怎样删除淘宝上的地址 编辑:程序博客网 时间:2024/04/28 05:09

Python os.SEEK_SETs 代码实例


实例 1


def fill(self):        """        Write the current file full and sync it to disk.  The goal here is        a contiguous area on disk for this file.        """        sys.stderr.write("[commitlog] filling commit log {0:010}n".format(            self.index        ))        # Preferred version would use syscall(2) to call fallocate(2) or        # directly call posix_fallocate(3).        #libc = ctypes.CDLL("libc.so.6")        #libc.syscall(285, self,fd, self.WRITE, 0, self.filesize)        #libc.posix_fallocate(self.fd, 0, self.filesize)        # But for now, I'll settle for writing a file full manually and        # hoping it's contiguous on disk.        empty = message.Write("" * message.Write.LENGTH)        while not self.full():            self.write(empty, False)        os.fsync(self.fd)        self.len = 0        os.lseek(self.fd, 0, os.SEEK_SET)     

实例 2


def seek(self, pos, whence=os.SEEK_SET):        """Seek to a position in the file.        """        if self.closed:            raise ValueError("I/O operation on closed file")        if whence == os.SEEK_SET:            self.position = min(max(pos, 0), self.size)        elif whence == os.SEEK_CUR:            if pos < 0:                self.position = max(self.position + pos, 0)            else:                self.position = min(self.position + pos, self.size)        elif whence == os.SEEK_END:            self.position = max(min(self.size + pos, self.size), 0)        else:            raise ValueError("Invalid argument")        self.buffer = ""        self.fileobj.seek(self.position)     

实例 3


def _GetRangeData(self, ranges):    """Generator that produces all the image data in 'ranges'.  The    number of individual pieces returned is arbitrary (and in    particular is not necessarily equal to the number of ranges in    'ranges'.    This generator is stateful -- it depends on the open file object    contained in this SparseImage, so you should not try to run two    instances of this generator on the same object simultaneously."""    f = self.simg_f    for s, e in ranges:      to_read = e-s      idx = bisect.bisect_right(self.offset_index, s) - 1      chunk_start, chunk_len, filepos, fill_data = self.offset_map[idx]      # for the first chunk we may be starting partway through it.      remain = chunk_len - (s - chunk_start)      this_read = min(remain, to_read)      if filepos is not None:        p = filepos + ((s - chunk_start) * self.blocksize)        f.seek(p, os.SEEK_SET)        yield f.read(this_read * self.blocksize)      else:        yield fill_data * (this_read * (self.blocksize >> 2))      to_read -= this_read      while to_read > 0:        # continue with following chunks if this range spans multiple chunks.        idx += 1        chunk_start, chunk_len, filepos, fill_data = self.offset_map[idx]        this_read = min(chunk_len, to_read)        if filepos is not None:          f.seek(filepos, os.SEEK_SET)          yield f.read(this_read * self.blocksize)        else:          yield fill_data * (this_read * (self.blocksize >> 2))        to_read -= this_read   

实例 4


def to_python(self, data):        d = D({})        d['filename'] = os.path.basename(data.filename)        d['file'] = data.file        data.file.seek(0, os.SEEK_END)        d['size'] = data.file.tell()        data.file.seek(0, os.SEEK_SET)        return d

原创粉丝点击