當前位置: 首頁>>代碼示例>>Python>>正文


Python os.SEEK_SET屬性代碼示例

本文整理匯總了Python中os.SEEK_SET屬性的典型用法代碼示例。如果您正苦於以下問題:Python os.SEEK_SET屬性的具體用法?Python os.SEEK_SET怎麽用?Python os.SEEK_SET使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在os的用法示例。


在下文中一共展示了os.SEEK_SET屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: seek

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
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 = b""
        self.fileobj.seek(self.position) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:22,代碼來源:tarfile.py

示例2: flush

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def flush(self):
        if self._dirty:
            size = self._root_block_size()
            self.allocate(size, 0)
            with self.get_block(0) as rblk:
                self._write_root_block_into(rblk)

            addr = self._offsets[0]
            offset = addr & ~0x1f
            size = 1 << (addr & 0x1f)

            self._file.seek(0, os.SEEK_SET)
            self._file.write(struct.pack(b'>I4sIII16s',
                                         1, b'Bud1',
                                         offset, size, offset,
                                         self._unknown1))

            self._dirty = False
            
        self._file.flush() 
開發者ID:al45tair,項目名稱:ds_store,代碼行數:22,代碼來源:buddy.py

示例3: annotation_stream

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
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 
開發者ID:irmen,項目名稱:Pyro5,代碼行數:21,代碼來源:server.py

示例4: seek

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
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) 
開發者ID:glmcdona,項目名稱:meddle,代碼行數:22,代碼來源:tarfile.py

示例5: add_data

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def add_data(self, name, data):
		"""
		Add arbitrary data directly to the archive under the specified name.
		This allows data to be directly inserted into the archive without first
		writing it to a file or file like object.

		:param str name: The name of the destination file in the archive.
		:param data: The data to place into the archive.
		:type data: bytes, str
		"""
		if its.py_v2 and isinstance(data, unicode):
			data = data.encode(self.encoding)
		elif its.py_v3 and isinstance(data, str):
			data = data.encode(self.encoding)
		pseudo_file = io.BytesIO()
		pseudo_file.write(data)

		tarinfo = tarfile.TarInfo(name=name)
		tarinfo.mtime = self.mtime
		tarinfo.size = pseudo_file.tell()
		pseudo_file.seek(os.SEEK_SET)
		self._tar_h.addfile(tarinfo=tarinfo, fileobj=pseudo_file) 
開發者ID:rsmusllp,項目名稱:king-phisher,代碼行數:24,代碼來源:archive.py

示例6: test_open

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def test_open(self):
        test_filename = "tmp.open.test"
        fd1 = os.open(test_filename + "1", flags)

        # make sure fd+1 and fd+2 are closed
        os.closerange(fd1 + 1, fd1 + 2)

        # open should return the lowest-numbered file descriptor not currently open
        # for the process
        fd2 = os.open(test_filename + "2", flags)
        fd3 = os.open(test_filename + "3", flags)

        os.close(fd2)
        self.assertRaisesMessage(OSError, "[Errno 9] Bad file descriptor", os.lseek, fd2, os.SEEK_SET, 0)

        fd4 = os.open(test_filename + "4", flags)
        self.assertEqual(fd4, fd2)

        os.close(fd1)
        os.close(fd3)
        os.close(fd4)

        for i in range(1, 5):
            os.unlink(test_filename + str(i)) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:26,代碼來源:test_fd.py

示例7: load_mnist

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def load_mnist(root, training):
    if training:
        data = 'train-images-idx3-ubyte'
        label = 'train-labels-idx1-ubyte'
        N = 60000
    else:
        data = 't10k-images-idx3-ubyte'
        label = 't10k-labels-idx1-ubyte'
        N = 10000
    with open(osp.join(root, data), 'rb') as fin:
        fin.seek(16, os.SEEK_SET)
        X = np.fromfile(fin, dtype=np.uint8).reshape((N, 28 * 28))
    with open(osp.join(root, label), 'rb') as fin:
        fin.seek(8, os.SEEK_SET)
        Y = np.fromfile(fin, dtype=np.uint8)
    return X, Y 
開發者ID:shahsohil,項目名稱:DCC,代碼行數:18,代碼來源:make_data.py

示例8: create_media

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def create_media(media):
    """Download media link"""
    if is_valid_url(media.data_value):
        filename = media.data_value.split('/')[-1]
        data_file = NamedTemporaryFile()
        content_type = mimetypes.guess_type(filename)
        with closing(requests.get(media.data_value, stream=True)) as r:
            for chunk in r.iter_content(chunk_size=CHUNK_SIZE):
                if chunk:
                    data_file.write(chunk)
        data_file.seek(os.SEEK_SET, os.SEEK_END)
        size = os.path.getsize(data_file.name)
        data_file.seek(os.SEEK_SET)
        media.data_value = filename
        media.data_file = InMemoryUploadedFile(
            data_file, 'data_file', filename, content_type,
            size, charset=None)

        return media

    return None 
開發者ID:awemulya,項目名稱:kobo-predict,代碼行數:23,代碼來源:meta_data.py

示例9: _set_hash

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def _set_hash(self):
        if not self.data_file:
            return None

        file_exists = self.data_file.storage.exists(self.data_file.name)

        if (file_exists and self.data_file.name != '') \
                or (not file_exists and self.data_file):
            try:
                self.data_file.seek(os.SEEK_SET)
            except IOError:
                return u''
            else:
                self.file_hash = u'md5:%s' \
                    % md5(self.data_file.read()).hexdigest()

                return self.file_hash

        return u'' 
開發者ID:awemulya,項目名稱:kobo-predict,代碼行數:21,代碼來源:meta_data.py

示例10: sidesum

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def sidesum(filename, chksize, bufsize, offset=0):
    if bufsize < chksize:
        bufsizes = (bufsize, chksize % bufsize)
    else:
        bufsizes = (chksize, 0)

    offset = abs(offset)

    with readopen(filename, sequential=False, direct=True) as (read, fd):
        whence = (offset, os.SEEK_SET)
        header = _chunksum(fd, read, chksize, bufsizes, whence)

        whence = (-chksize - offset, os.SEEK_END)
        footer = _chunksum(fd, read, chksize, bufsizes, whence)

    return header, footer 
開發者ID:deplicate,項目名稱:deplicate,代碼行數:18,代碼來源:common.py

示例11: read_from_handle_truncated

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def read_from_handle_truncated(file_handle, max_len):
  """Read from file handle, limiting output to |max_len| by removing output in
  the middle."""
  file_handle.seek(0, os.SEEK_END)
  file_size = file_handle.tell()
  file_handle.seek(0, os.SEEK_SET)

  if file_size <= max_len:
    return file_handle.read()

  # Read first and last |half_max_len| bytes.
  half_max_len = max_len // 2
  start = file_handle.read(half_max_len)
  file_handle.seek(file_size - half_max_len, os.SEEK_SET)
  end = file_handle.read(half_max_len)

  truncated_marker = b'\n...truncated %d bytes...\n' % (file_size - max_len)

  return start + truncated_marker + end 
開發者ID:google,項目名稱:clusterfuzz,代碼行數:21,代碼來源:utils.py

示例12: _get_org

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def _get_org(self, ipnum):
        """
        Seek and return organization or ISP name for ipnum.
        Return org/isp name.

        :arg ipnum: Result of ip2long conversion
        """
        seek_org = self._seek_country(ipnum)
        if seek_org == self._databaseSegments:
            return None

        read_length = (2 * self._recordLength - 1) * self._databaseSegments
        try:
            self._lock.acquire()
            self._fp.seek(seek_org + read_length, os.SEEK_SET)
            buf = self._fp.read(const.MAX_ORG_RECORD_LENGTH)
        finally:
            self._lock.release()

        if PY3 and type(buf) is bytes:
            buf = buf.decode(ENCODING)

        return buf[:buf.index(chr(0))] 
開發者ID:medbenali,項目名稱:CyberScan,代碼行數:25,代碼來源:__init__.py

示例13: connect

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def connect(self, start: int, headers: bytes) -> int:
        added = 0
        bail = False
        loop = asyncio.get_running_loop()
        async with self._header_connect_lock:
            for height, chunk in self._iterate_chunks(start, headers):
                try:
                    # validate_chunk() is CPU bound and reads previous chunks from file system
                    await loop.run_in_executor(None, self.validate_chunk, height, chunk)
                except InvalidHeader as e:
                    bail = True
                    chunk = chunk[:(height-e.height)*self.header_size]
                written = 0
                if chunk:
                    self.io.seek(height * self.header_size, os.SEEK_SET)
                    written = self.io.write(chunk) // self.header_size
                    self.io.truncate()
                    # .seek()/.write()/.truncate() might also .flush() when needed
                    # the goal here is mainly to ensure we're definitely flush()'ing
                    await loop.run_in_executor(None, self.io.flush)
                    self._size = None
                added += written
                if bail:
                    break
        return added 
開發者ID:lbryio,項目名稱:torba,代碼行數:27,代碼來源:baseheader.py

示例14: read

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def read(self):
        """Read the brightness of the LED.

        Returns:
            int: Current brightness.

        Raises:
            LEDError: if an I/O or OS error occurs.

        """
        # Read value
        try:
            buf = os.read(self._fd, 8)
        except OSError as e:
            raise LEDError(e.errno, "Reading LED brightness: " + e.strerror)

        # Rewind
        try:
            os.lseek(self._fd, 0, os.SEEK_SET)
        except OSError as e:
            raise LEDError(e.errno, "Rewinding LED brightness: " + e.strerror)

        return int(buf) 
開發者ID:vsergeev,項目名稱:python-periphery,代碼行數:25,代碼來源:led.py

示例15: read

# 需要導入模塊: import os [as 別名]
# 或者: from os import SEEK_SET [as 別名]
def read(self):
        # Read value
        try:
            buf = os.read(self._fd, 2)
        except OSError as e:
            raise GPIOError(e.errno, "Reading GPIO: " + e.strerror)

        # Rewind
        try:
            os.lseek(self._fd, 0, os.SEEK_SET)
        except OSError as e:
            raise GPIOError(e.errno, "Rewinding GPIO: " + e.strerror)

        if buf[0] == b"0"[0]:
            return False
        elif buf[0] == b"1"[0]:
            return True

        raise GPIOError(None, "Unknown GPIO value: {}".format(buf)) 
開發者ID:vsergeev,項目名稱:python-periphery,代碼行數:21,代碼來源:gpio.py


注:本文中的os.SEEK_SET屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。