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
阅读全文
0 0
- Python os.SEEK_SETs 代码实例
- Python: os模块实例详解
- python代码refine实例
- python多线程代码实例
- [代码笔记] python 之模块OS
- PYTHON操作EXCEL,实例代码
- Python操作Mysql实例代码
- python smtp模块实例代码
- Python操作Mysql实例代码
- python操作mysql实例代码
- python socket的代码实例
- Python urllib2实例代码演示
- python开发爬虫实例代码
- appium python client代码实例
- python代码发送邮件实例
- Python urllib.urlretrieves 代码实例
- Python标准库os.path包、glob包使用实例
- python中os操作文件及文件路径实例汇总
- Java基础总结
- KDTree简单理解
- ClickOnce 发布WinForm应用程序(非签名方式)
- XRecyclerView使用
- 数据结构:编写病人看病模拟程序(C/C 艹)
- Python os.SEEK_SETs 代码实例
- Ubuntu16.04 terminate called after throwing an instance of 'std::logic_error'
- 实验四 Linux系统管理
- Ubuntu16.04 安装Boost与Opencv及CMakeLists.txt配置
- Python入门(3)
- 实验二 栈和队列——顺序栈、顺序队列
- Python中的socket网络编程(TCP/IP,UDP)讲解
- 处理图片缩放遇到的问题
- [noip2015]运输计划 题解