Python io.SEEK_SET Examples
The following are 30
code examples of io.SEEK_SET().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
io
, or try the search function
.
Example #1
Source File: gzip.py From Imogen with MIT License | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): if self.mode == WRITE: if whence != io.SEEK_SET: if whence == io.SEEK_CUR: offset = self.offset + offset else: raise ValueError('Seek from end not supported') if offset < self.offset: raise OSError('Negative seek in write mode') count = offset - self.offset chunk = b'\0' * 1024 for i in range(count // 1024): self.write(chunk) self.write(b'\0' * (count % 1024)) elif self.mode == READ: self._check_not_closed() return self._buffer.seek(offset, whence) return self.offset
Example #2
Source File: test_anim_encoder.py From pycozmo with MIT License | 6 votes |
def test_no_clips(self): data = json.loads(""" { "clips": [] } """) # Load from JSON clips = AnimClips.from_dict(data) self.assertEqual(len(clips.clips), 0) # Save to FB f = io.BytesIO() clips.to_fb_stream(f) f.seek(0, io.SEEK_SET) # Load from FB clips = clips.from_fb_stream(f) self.assertEqual(len(clips.clips), 0) # Save to JSON data2 = clips.to_dict() self.assertEqual(data, data2)
Example #3
Source File: lzma.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): """Change the file position. The new position is specified by offset, relative to the position indicated by whence. Possible values for whence are: 0: start of stream (default): offset must not be negative 1: current stream position 2: end of stream; offset must not be positive Returns the new file position. Note that seeking is emulated, so depending on the parameters, this operation may be extremely slow. """ self._check_can_seek() return self._buffer.seek(offset, whence)
Example #4
Source File: qtutils.py From qutebrowser with GNU General Public License v3.0 | 6 votes |
def seek(self, offset: int, whence: int = io.SEEK_SET) -> int: self._check_open() self._check_random() if whence == io.SEEK_SET: ok = self.dev.seek(offset) elif whence == io.SEEK_CUR: ok = self.dev.seek(self.tell() + offset) elif whence == io.SEEK_END: ok = self.dev.seek(len(self) + offset) else: raise io.UnsupportedOperation("whence = {} is not " "supported!".format(whence)) if not ok: raise QtOSError(self.dev, msg="seek failed!") return self.dev.pos()
Example #5
Source File: VirtualFile.py From VideoSuperResolution with MIT License | 6 votes |
def seek(self, offset, where=SEEK_SET): """Seek the position by `offset` relative to `where`. Args: offset: move the read pointer by `offset` bytes. where: same as io.SEEK_END, io.SEEK_CUR or io.SEEK_SET. """ if where == SEEK_CUR: cur = len(self.read_file) pos = cur + offset elif where == SEEK_END: pos = len(self.read_file) + len(self.file) + offset else: pos = offset if pos < 0: pos = 0 self.file = self.read_file + self.file self.read_file = self.file[:pos] self.file = self.file[pos:] self.cur_fd = None
Example #6
Source File: mkdz.py From kdztools with GNU General Public License v3.0 | 6 votes |
def write(self, file, name): """ Write our block to the file with the specified name """ input = io.FileIO(self.name, "rb") l = input.seek(0, io.SEEK_END) input.seek(0, io.SEEK_SET) print("[+] Writing {:s} to {:s} ({:d} bytes)".format(self.name, name, l)) buf = b" " while len(buf) > 0: buf = input.read(4096) file.write(buf) input.close()
Example #7
Source File: utils.py From pyclibrary with MIT License | 6 votes |
def get_shared_library_arch(filename): with io.open(filename, 'rb') as fp: dos_headers = fp.read(64) fp.read(4) magic, skip, offset = _struct_unpack(str('2s58sl'), dos_headers) if magic != b'MZ': raise Exception('Not an executable') fp.seek(offset, io.SEEK_SET) pe_header = fp.read(6) sig, skip, machine = _struct_unpack(str('2s2sH'), pe_header) if sig != b'PE': raise Exception('Not a PE executable') return machine_types.get(machine, 'UNKNOWN')
Example #8
Source File: gzip.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): if self.mode == WRITE: if whence != io.SEEK_SET: if whence == io.SEEK_CUR: offset = self.offset + offset else: raise ValueError('Seek from end not supported') if offset < self.offset: raise OSError('Negative seek in write mode') count = offset - self.offset chunk = bytes(1024) for i in range(count // 1024): self.write(chunk) self.write(bytes(count % 1024)) elif self.mode == READ: self._check_not_closed() return self._buffer.seek(offset, whence) return self.offset
Example #9
Source File: bz2.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): """Change the file position. The new position is specified by offset, relative to the position indicated by whence. Values for whence are: 0: start of stream (default); offset must not be negative 1: current stream position 2: end of stream; offset must not be positive Returns the new file position. Note that seeking is emulated, so depending on the parameters, this operation may be extremely slow. """ with self._lock: self._check_can_seek() return self._buffer.seek(offset, whence)
Example #10
Source File: server.py From Pyro5 with MIT License | 6 votes |
def annotation_stream(self, with_checksum=False): # create a large temporary file f = tempfile.TemporaryFile() for _ in range(5000): f.write(b"1234567890!" * 1000) filesize = f.tell() f.seek(os.SEEK_SET, 0) # return the file data via annotation stream (remote iterator) annotation_size = 500000 print("transmitting file via annotations stream (%d bytes in chunks of %d)..." % (filesize, annotation_size)) with f: while True: chunk = f.read(annotation_size) if not chunk: break # store the file data chunk in the FDAT response annotation, # and return the current file position and checksum (if asked). current_context.response_annotations = {"FDAT": chunk} yield f.tell(), zlib.crc32(chunk) if with_checksum else 0
Example #11
Source File: lobs.py From PyHDB with Apache License 2.0 | 6 votes |
def read(self, n=-1): """Read up to n items (bytes/chars) from the lob and return them. If n is -1 then all available data is returned. Might trigger further loading of data from the database if the number of items requested for reading is larger than what is currently buffered. """ pos = self.tell() num_items_to_read = n if n != -1 else self.length - pos # calculate the position of the file pointer after data was read: new_pos = min(pos + num_items_to_read, self.length) if new_pos > self._current_lob_length: missing_num_items_to_read = new_pos - self._current_lob_length self._read_missing_lob_data_from_db(self._current_lob_length, missing_num_items_to_read) # reposition file pointer to original position as reading in IO buffer might have changed it self.seek(pos, SEEK_SET) return self.data.read(n)
Example #12
Source File: lobs.py From PyHDB with Apache License 2.0 | 6 votes |
def seek(self, offset, whence=SEEK_SET): """Seek pointer in lob data buffer to requested position. Might trigger further loading of data from the database if the pointer is beyond currently read data. """ # A nice trick is to (ab)use BytesIO.seek() to go to the desired position for easier calculation. # This will not add any data to the buffer however - very convenient! self.data.seek(offset, whence) new_pos = self.data.tell() missing_bytes_to_read = new_pos - self._current_lob_length if missing_bytes_to_read > 0: # Trying to seek beyond currently available LOB data, so need to load some more first. # We are smart here: (at least trying...): # If a user sets a certain file position s/he probably wants to read data from # there. So already read some extra data to avoid yet another immediate # reading step. Try with EXTRA_NUM_ITEMS_TO_READ_AFTER_SEEK additional items (bytes/chars). # jump to the end of the current buffer and read the new data: self.data.seek(0, SEEK_END) self.read(missing_bytes_to_read + self.EXTRA_NUM_ITEMS_TO_READ_AFTER_SEEK) # reposition file pointer a originally desired position: self.data.seek(new_pos) return new_pos
Example #13
Source File: ratarmount.py From ratarmount with MIT License | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): if whence == io.SEEK_CUR: self.offset += offset elif whence == io.SEEK_END: self.offset = self.cumsizes[-1] + offset elif whence == io.SEEK_SET: self.offset = offset if self.offset < 0: raise Exception("Trying to seek before the start of the file!") if self.offset >= self.cumsizes[-1]: return self.offset i = self._findStencil( self.offset ) offsetInsideStencil = self.offset - self.cumsizes[i] assert offsetInsideStencil >= 0 assert offsetInsideStencil < self.sizes[i] self.fileobj.seek( self.offsets[i] + offsetInsideStencil, io.SEEK_SET ) return self.offset
Example #14
Source File: cfb.py From pyaaf2 with MIT License | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): if whence == io.SEEK_CUR: offset = self.tell() + offset elif whence == io.SEEK_END: offset = self.dir.byte_size + offset if offset < 0: raise ValueError('New position is before the start of the stream') if offset > self.dir.byte_size: # logging.debug("overseek %d bytes, padding with zeros" % (offset - self.dir.byte_size)) self.pos = self.dir.byte_size bytes_left = offset - self.dir.byte_size min_seek_size = self.storage.sector_size * 4 while bytes_left: bytes_to_write = min(min_seek_size, offset - self.dir.byte_size) zeros = bytearray(bytes_to_write) self.write(zeros) bytes_left -= bytes_to_write self.pos = offset return offset
Example #15
Source File: ContainerIO.py From teleport with Apache License 2.0 | 6 votes |
def seek(self, offset, mode=io.SEEK_SET): """ Move file pointer. :param offset: Offset in bytes. :param mode: Starting position. Use 0 for beginning of region, 1 for current offset, and 2 for end of region. You cannot move the pointer outside the defined region. """ if mode == 1: self.pos = self.pos + offset elif mode == 2: self.pos = self.length + offset else: self.pos = offset # clamp self.pos = max(0, min(self.pos, self.length)) self.fh.seek(self.offset + self.pos)
Example #16
Source File: test_file_slice.py From onedrive-sdk-python with MIT License | 6 votes |
def testSanityChecks(self): with tempfile.TemporaryFile() as f: f.write(b'123456789') f.flush() with self.assertRaises(ValueError): part = FileSlice(f, -5, -2) with self.assertRaises(ValueError): part = FileSlice(f, 0, -2) with self.assertRaises(ValueError): part = FileSlice(f, -10, 2) with self.assertRaises(ValueError): part = FileSlice(f, 10, 2) with self.assertRaises(ValueError): part = FileSlice(f, 10, length=-2) part = FileSlice(f, 1, 5) with self.assertRaises(ValueError): part.seek(8) with self.assertRaises(ValueError): part.seek(8, io.SEEK_SET) part.seek(3) with self.assertRaises(ValueError): part.seek(4, io.SEEK_CUR) with self.assertRaises(ValueError): part.seek(-5, io.SEEK_END)
Example #17
Source File: TiffImagePlugin.py From teleport with Apache License 2.0 | 6 votes |
def setup(self): # Reset everything. self.f.seek(self.beginning, os.SEEK_SET) self.whereToWriteNewIFDOffset = None self.offsetOfNewPage = 0 self.IIMM = IIMM = self.f.read(4) if not IIMM: # empty file - first page self.isFirst = True return self.isFirst = False if IIMM == b"II\x2a\x00": self.setEndian("<") elif IIMM == b"MM\x00\x2a": self.setEndian(">") else: raise RuntimeError("Invalid TIFF file header") self.skipIFDs() self.goToEnd()
Example #18
Source File: ContainerIO.py From teleport with Apache License 2.0 | 6 votes |
def seek(self, offset, mode=io.SEEK_SET): """ Move file pointer. :param offset: Offset in bytes. :param mode: Starting position. Use 0 for beginning of region, 1 for current offset, and 2 for end of region. You cannot move the pointer outside the defined region. """ if mode == 1: self.pos = self.pos + offset elif mode == 2: self.pos = self.length + offset else: self.pos = offset # clamp self.pos = max(0, min(self.pos, self.length)) self.fh.seek(self.offset + self.pos)
Example #19
Source File: TiffImagePlugin.py From teleport with Apache License 2.0 | 6 votes |
def setup(self): # Reset everything. self.f.seek(self.beginning, os.SEEK_SET) self.whereToWriteNewIFDOffset = None self.offsetOfNewPage = 0 self.IIMM = IIMM = self.f.read(4) if not IIMM: # empty file - first page self.isFirst = True return self.isFirst = False if IIMM == b"II\x2a\x00": self.setEndian("<") elif IIMM == b"MM\x00\x2a": self.setEndian(">") else: raise RuntimeError("Invalid TIFF file header") self.skipIFDs() self.goToEnd()
Example #20
Source File: test_file_slice.py From onedrive-sdk-python with MIT License | 6 votes |
def testSliceFileStartEnd(self): with tempfile.TemporaryFile() as f: f.write(b'123456789') f.flush() part = FileSlice(f, 0, 5) self.assertEqual(len(part), 5) self.assertEqual(part.read(), b'12345') self.assertEqual(part.read(3), b'') part.seek(0, io.SEEK_SET) self.assertEqual(part.read(3), b'123') self.assertEqual(part.tell(), 3) part.seek(-3, io.SEEK_CUR) self.assertEqual(part.tell(), 0) part.seek(-2, io.SEEK_END) self.assertEqual(part.tell(), 3) self.assertEqual(part.readall(), b'45') with self.assertRaises(IOError): part.write('abc') with self.assertRaises(IOError): part.writelines(['foo', 'bar'])
Example #21
Source File: ContainerIO.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 6 votes |
def seek(self, offset, mode=io.SEEK_SET): """ Move file pointer. :param offset: Offset in bytes. :param mode: Starting position. Use 0 for beginning of region, 1 for current offset, and 2 for end of region. You cannot move the pointer outside the defined region. """ if mode == 1: self.pos = self.pos + offset elif mode == 2: self.pos = self.length + offset else: self.pos = offset # clamp self.pos = max(0, min(self.pos, self.length)) self.fh.seek(self.offset + self.pos)
Example #22
Source File: TiffImagePlugin.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 6 votes |
def setup(self): # Reset everything. self.f.seek(self.beginning, os.SEEK_SET) self.whereToWriteNewIFDOffset = None self.offsetOfNewPage = 0 self.IIMM = IIMM = self.f.read(4) if not IIMM: # empty file - first page self.isFirst = True return self.isFirst = False if IIMM == b"II\x2a\x00": self.setEndian("<") elif IIMM == b"MM\x00\x2a": self.setEndian(">") else: raise RuntimeError("Invalid TIFF file header") self.skipIFDs() self.goToEnd()
Example #23
Source File: bz2.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): """Change the file position. The new position is specified by offset, relative to the position indicated by whence. Values for whence are: 0: start of stream (default); offset must not be negative 1: current stream position 2: end of stream; offset must not be positive Returns the new file position. Note that seeking is emulated, so depending on the parameters, this operation may be extremely slow. """ with self._lock: self._check_can_seek() return self._buffer.seek(offset, whence)
Example #24
Source File: lzma.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): """Change the file position. The new position is specified by offset, relative to the position indicated by whence. Possible values for whence are: 0: start of stream (default): offset must not be negative 1: current stream position 2: end of stream; offset must not be positive Returns the new file position. Note that seeking is emulated, so depending on the parameters, this operation may be extremely slow. """ self._check_can_seek() return self._buffer.seek(offset, whence)
Example #25
Source File: gzip.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): if self.mode == WRITE: if whence != io.SEEK_SET: if whence == io.SEEK_CUR: offset = self.offset + offset else: raise ValueError('Seek from end not supported') if offset < self.offset: raise OSError('Negative seek in write mode') count = offset - self.offset chunk = bytes(1024) for i in range(count // 1024): self.write(chunk) self.write(bytes(count % 1024)) elif self.mode == READ: self._check_not_closed() return self._buffer.seek(offset, whence) return self.offset
Example #26
Source File: bz2.py From Imogen with MIT License | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): """Change the file position. The new position is specified by offset, relative to the position indicated by whence. Values for whence are: 0: start of stream (default); offset must not be negative 1: current stream position 2: end of stream; offset must not be positive Returns the new file position. Note that seeking is emulated, so depending on the parameters, this operation may be extremely slow. """ with self._lock: self._check_can_seek() return self._buffer.seek(offset, whence)
Example #27
Source File: lzma.py From Imogen with MIT License | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): """Change the file position. The new position is specified by offset, relative to the position indicated by whence. Possible values for whence are: 0: start of stream (default): offset must not be negative 1: current stream position 2: end of stream; offset must not be positive Returns the new file position. Note that seeking is emulated, so depending on the parameters, this operation may be extremely slow. """ self._check_can_seek() return self._buffer.seek(offset, whence)
Example #28
Source File: file_slice.py From onedrive-sdk-python with MIT License | 6 votes |
def seek(self, offset, whence=io.SEEK_SET): if whence == io.SEEK_SET: desired_pos = self._start + offset if whence == io.SEEK_CUR: desired_pos = self._handle.tell() + offset if whence == io.SEEK_END: desired_pos = self._end + offset if desired_pos < self._start: raise ValueError("Seeking before the file slice") if desired_pos > self._end: raise ValueError("Seekeing past the end of file slice") ret = self._handle.seek(desired_pos, io.SEEK_SET) if ret: return ret - self._start else: return ret
Example #29
Source File: pathio.py From aioftp with Apache License 2.0 | 5 votes |
def seek(self, file, offset, whence=io.SEEK_SET): """ :py:func:`asyncio.coroutine` Change the stream position to the given byte `offset`. Same behaviour as :py:meth:`io.IOBase.seek` :param file: file-object from :py:class:`aioftp.AbstractPathIO.open` :param offset: relative byte offset :type offset: :py:class:`int` :param whence: base position for offset :type whence: :py:class:`int` """
Example #30
Source File: challenge.py From msldap with MIT License | 5 votes |
def from_buffer(buff): t = NTLMChallenge() t.Signature = buff.read(8) t.MessageType = int.from_bytes(buff.read(4), byteorder = 'little', signed = False) t.TargetNameFields = Fields.from_buffer(buff) t.NegotiateFlags = NegotiateFlags(int.from_bytes(buff.read(4), byteorder = 'little', signed = False)) t.ServerChallenge = buff.read(8) t.Reserved = buff.read(8) t.TargetInfoFields = Fields.from_buffer(buff) if t.NegotiateFlags & NegotiateFlags.NEGOTIATE_VERSION: t.Version = Version.from_buffer(buff) currPos = buff.tell() t.Payload = buff.read() if t.TargetNameFields.length != 0: buff.seek(t.TargetNameFields.offset, io.SEEK_SET) raw_data = buff.read(t.TargetNameFields.length) try: t.TargetName = raw_data.decode('utf-16le') except UnicodeDecodeError: # yet another cool bug. t.TargetName = raw_data.decode('utf-8') if t.TargetInfoFields.length != 0: buff.seek(t.TargetInfoFields.offset, io.SEEK_SET) raw_data = buff.read(t.TargetInfoFields.length) t.TargetInfo = AVPairs.from_bytes(raw_data) return t