Python os.SEEK_SETs 代码实例

本文通过四个具体的Python代码实例介绍了如何使用os.SEEK_SET进行文件操作,包括文件填充、定位、读取范围数据及文件元数据获取等常见应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值