本文整理汇总了Python中os.lseek方法的典型用法代码示例。如果您正苦于以下问题:Python os.lseek方法的具体用法?Python os.lseek怎么用?Python os.lseek使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.lseek方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_open
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [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))
示例2: lseek
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def lseek(self, port, pos, how):
"""
Use lseek on the device. The device is unseekable so PASS is returned
when lseek command fails and vice versa.
:param port: Name of the port
:param pos: Offset
:param how: Relative offset os.SEEK_{SET,CUR,END}
"""
fd = self._open([port])[0]
try:
os.lseek(fd, pos, how)
except Exception as inst:
if inst.errno == 29:
print("PASS: the lseek failed as expected")
else:
print(inst)
print("FAIL: unknown error")
else:
print("FAIL: the lseek unexpectedly passed")
示例3: _chunksum
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def _chunksum(fd, read, size, bufsizes, whence):
buf0, buf1 = bufsizes
offset, how = whence
x = _xxhash_xxh()
update = x.update
if offset:
os.lseek(fd, offset, how)
left = size
data = read(buf0)
while left and data:
update(data)
left -= buf0
data = read(buf0)
if buf1:
data = read(buf1)
update(data)
return x.hexdigest()
示例4: read
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [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)
示例5: read
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [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))
示例6: write
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def write(self, value):
if not isinstance(value, bool):
raise TypeError("Invalid value type, should be bool.")
# Write value
try:
if value:
os.write(self._fd, b"1\n")
else:
os.write(self._fd, b"0\n")
except OSError as e:
raise GPIOError(e.errno, "Writing 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)
示例7: _get_loop_size
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def _get_loop_size(self, path):
sudo = [] if os.getuid() == 0 else ['sudo']
try:
loop_name = subprocess.check_output(
sudo + ['losetup', '--associated', path]).decode().split(':')[0]
if os.getuid() != 0:
return int(
subprocess.check_output(
['sudo', 'blockdev', '--getsize64', loop_name]))
fd = os.open(loop_name, os.O_RDONLY)
try:
return os.lseek(fd, 0, os.SEEK_END)
finally:
os.close(fd)
except subprocess.CalledProcessError:
return None
示例8: loadFileSliceDataset
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def loadFileSliceDataset(datasetDescriptor, vdid, vdm):
fd = None
path = datasetDescriptor.asFileSliceDataset.file.path
lowIndex = datasetDescriptor.asFileSliceDataset.lowOffset
highIndex = datasetDescriptor.asFileSliceDataset.highOffset
try:
fd = os.open(path, os.O_RDONLY)
os.lseek(fd, lowIndex, os.SEEK_SET)
if not vdm.loadByteArrayIntoExternalDatasetPageFromFileDescriptor(
vdid,
fd,
highIndex - lowIndex):
raise DatasetLoadException("Coulnd't load file slice into VDM")
except os.error as e:
message = 'Error loading file slice dataset: %s, %d-%d:\n%s' % (
path,
lowIndex,
highIndex,
e)
logging.error(message)
raise DatasetLoadException(message)
finally:
if fd is not None:
os.close(fd)
示例9: test_writev
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def test_writev(self):
fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
try:
n = os.writev(fd, (b'test1', b'tt2', b't3'))
self.assertEqual(n, 10)
os.lseek(fd, 0, os.SEEK_SET)
self.assertEqual(b'test1tt2t3', posix.read(fd, 10))
# Issue #20113: empty list of buffers should not crash
try:
size = posix.writev(fd, [])
except OSError:
# writev(fd, []) raises OSError(22, "Invalid argument")
# on OpenIndiana
pass
else:
self.assertEqual(size, 0)
finally:
os.close(fd)
示例10: test_fs_holes
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def test_fs_holes(self):
# Even if the filesystem doesn't report holes,
# if the OS supports it the SEEK_* constants
# will be defined and will have a consistent
# behaviour:
# os.SEEK_DATA = current position
# os.SEEK_HOLE = end of file position
with open(support.TESTFN, 'r+b') as fp:
fp.write(b"hello")
fp.flush()
size = fp.tell()
fno = fp.fileno()
try :
for i in range(size):
self.assertEqual(i, os.lseek(fno, i, os.SEEK_DATA))
self.assertLessEqual(size, os.lseek(fno, i, os.SEEK_HOLE))
self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_DATA)
self.assertRaises(OSError, os.lseek, fno, size, os.SEEK_HOLE)
except OSError :
# Some OSs claim to support SEEK_HOLE/SEEK_DATA
# but it is not true.
# For instance:
# http://lists.freebsd.org/pipermail/freebsd-amd64/2012-January/014332.html
raise unittest.SkipTest("OSError raised!")
示例11: writemsr
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def writemsr(msr, val, cpu = -1):
try:
if cpu == -1:
for c in glob.glob('/dev/cpu/[0-9]*/msr'):
f = os.open(c, os.O_WRONLY)
os.lseek(f, msr, os.SEEK_SET)
os.write(f, struct.pack('Q', val))
os.close(f)
else:
f = os.open('/dev/cpu/%d/msr' % (cpu), os.O_WRONLY)
os.lseek(f, msr, os.SEEK_SET)
os.write(f, struct.pack('Q', val))
os.close(f)
except:
raise OSError("msr module not loaded (run modprobe msr)")
示例12: readmsr
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def readmsr(msr, cpu = 0):
try:
f = os.open('/dev/cpu/%d/msr' % cpu, os.O_RDONLY)
os.lseek(f, msr, os.SEEK_SET)
val = struct.unpack('Q', os.read(f, 8))[0]
os.close(f)
return val
except:
raise OSError("msr module not loaded (run modprobe msr)")
示例13: test_lseek
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def test_lseek(self):
if verbose:
print('play around with os.lseek() with the built largefile')
with self.open(TESTFN, 'rb') as f:
self.assertEqual(os.lseek(f.fileno(), 0, 0), 0)
self.assertEqual(os.lseek(f.fileno(), 42, 0), 42)
self.assertEqual(os.lseek(f.fileno(), 42, 1), 84)
self.assertEqual(os.lseek(f.fileno(), 0, 1), 84)
self.assertEqual(os.lseek(f.fileno(), 0, 2), size+1+0)
self.assertEqual(os.lseek(f.fileno(), -10, 2), size+1-10)
self.assertEqual(os.lseek(f.fileno(), -size-1, 2), 0)
self.assertEqual(os.lseek(f.fileno(), size, 0), size)
# the 'a' that was written at the end of file above
self.assertEqual(f.read(1), b'a')
示例14: test_stdin_filedes
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def test_stdin_filedes(self):
# stdin is set to open file descriptor
tf = tempfile.TemporaryFile()
d = tf.fileno()
os.write(d, "pear")
os.lseek(d, 0, 0)
p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.exit(sys.stdin.read() == "pear")'],
stdin=d)
p.wait()
self.assertEqual(p.returncode, 1)
示例15: test_stdout_filedes
# 需要导入模块: import os [as 别名]
# 或者: from os import lseek [as 别名]
def test_stdout_filedes(self):
# stdout is set to open file descriptor
tf = tempfile.TemporaryFile()
d = tf.fileno()
p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.stdout.write("orange")'],
stdout=d)
p.wait()
os.lseek(d, 0, 0)
self.assertEqual(os.read(d, 1024), "orange")