本文整理汇总了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)
示例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()
示例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
示例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)
示例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)
示例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))
示例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
示例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
示例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''
示例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
示例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
示例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))]
示例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
示例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)
示例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))