本文整理汇总了Python中io.FileIO.read方法的典型用法代码示例。如果您正苦于以下问题:Python FileIO.read方法的具体用法?Python FileIO.read怎么用?Python FileIO.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.FileIO
的用法示例。
在下文中一共展示了FileIO.read方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_from_file_memory_duplicate
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import read [as 别名]
def get_from_file_memory_duplicate(path):
io = FileIO(path,'rb')
io2 = StringIO()
io2.write(io.read())
io.close()
io2.seek(0, os.SEEK_SET)
return ELF(io2)
示例2: flipByteAt
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import read [as 别名]
def flipByteAt(inputfile, position):
"""Flips the bits for the byte at the specified position in the input file."""
f = FileIO(inputfile, "r+")
f.seek(position)
byte = ord(f.read(1))
f.seek(-1, 1) # go back 1 byte from current position
f.write(struct.pack("B", byte^0xFF)) # read in the byte and XOR it
f.close()
示例3: assertFlippedByte
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import read [as 别名]
def assertFlippedByte(self, file_orig, file_modded, position):
len_orig = os.path.getsize(file_orig)
len_modded = os.path.getsize(file_modded)
self.assertEqual(len_orig, len_modded, "Files of different sizes")
f_o = FileIO(file_orig, "r+b")
f_m = FileIO(file_modded, "r+b")
for i in xrange(len_orig):
# read in a byte from each file and compare
b_o = ord(f_o.read(1))
b_m = ord(f_m.read(1))
if i==position:
self.assertEqual(b_m, b_o^0xff, "Flipped bytes are actually equal at position "+str(i))
else:
self.assertEqual(b_o, b_m, "Bytes differ (when the shouldn't) at position "+str(i))
f_o.close()
f_m.close()
示例4: flipBitAt
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import read [as 别名]
def flipBitAt(inputfile, position):
"""Flips the bit at the specified position in the input file."""
if not 0<=position<(8*os.path.getsize(inputfile)):
raise IndexError("Position "+str(position)+" is out of range")
f = FileIO(inputfile, "r+")
f.seek(position/8)
byte = ord(f.read(1))
f.seek(-1, 1) # go back 1 byte from the current position
bitnum = position%8
f.write(struct.pack("B", byte^(1<<(7-bitnum))))
f.close()
示例5: read_from_file
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import read [as 别名]
def read_from_file(file_fd: io.FileIO, start: int, stop: int) -> bytes:
length = stop - start
assert length >= 0
file_fd.seek(start)
data = bytes()
while file_fd.tell() < stop:
read_data = file_fd.read(stop - file_fd.tell())
if read_data == b'':
raise ReachedEndOfFile('Read until the end of file')
data += read_data
assert len(data) == length
return data
示例6: load
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import read [as 别名]
def load(self, file: FileIO):
self.ptr = file.tell()
self.is_leaf, self.keys = load(file)
ptr_num = len(self.keys)
if not self.is_leaf:
ptr_num += (ptr_num + 1)
ptrs = unpack('Q' * ptr_num, file.read(8 * ptr_num))
if self.is_leaf:
self.ptrs_value = list(ptrs)
else:
self.ptrs_value = list(ptrs[:len(self.keys)])
self.ptrs_child = list(ptrs[len(self.keys):])
self.size = file.tell() - self.ptr
示例7: assertFlippedBit
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import read [as 别名]
def assertFlippedBit(self, file_orig, file_modded, position):
len_orig = os.path.getsize(file_orig)
len_modded = os.path.getsize(file_modded)
self.assertEqual(len_orig, len_modded, "Files of different sizes")
f_o = FileIO(file_orig, "r+b")
f_m = FileIO(file_modded, "r+b")
for i in xrange(len_orig):
# read in a byte from each file and compare
b_o = ord(f_o.read(1))
b_m = ord(f_m.read(1))
if i==(position/8):
for m in xrange(8):
bit_m = BitwiseAnalyser.BitManipulator.getBitFromByteAt(b_m, m)
bit_o = BitwiseAnalyser.BitManipulator.getBitFromByteAt(b_o, m)
if m==(position%8):
self.assertNotEqual(bit_m, bit_o, "Bits are equal when the should be different at position: "+str(position))
else:
self.assertEqual(bit_m, bit_o, "Bits are incorrectly different at position "+str(i))
else:
self.assertEqual(b_o, b_m, "Bytes differ (when the shouldn't) at position "+str(i))
f_o.close()
f_m.close()
示例8: FileDataReader
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import read [as 别名]
class FileDataReader(AbstractDataReader):
""" A reader that can read data from a file
"""
def __init__(self, filename):
"""
:param filename: The file to read
:type filename: str
:raise spinnman.exceptions.SpinnmanIOException: If the file\
cannot found or opened for reading
"""
try:
self._fileio = FileIO(filename, "r")
except IOError as e:
raise SpinnmanIOException(str(e))
def read(self, n_bytes):
""" See :py:meth:`spinnman.data.abstract_data_reader.AbstractDataReader.read`
"""
return bytearray(self._fileio.read(n_bytes))
def readinto(self, data):
""" See :py:meth:`spinnman.data.abstract_data_reader.AbstractDataReader.readinto`
"""
return self._fileio.readinto(data)
def readall(self):
""" See :py:meth:`spinnman.data.abstract_data_reader.AbstractDataReader.readall`
"""
return self._fileio.readall()
def close(self):
""" Closes the file
:return: Nothing is returned:
:rtype: None
:raise spinnman.exceptions.SpinnmanIOException: If the file\
cannot be closed
"""
try:
self._fileio.close()
except IOError as e:
raise SpinnmanIOException(str(e))
示例9: read
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import read [as 别名]
def read(self, n, *args):
self.prog.progressBy(n)
return FileIO.read(self, n, *args)
示例10: load
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import read [as 别名]
def load(self, file: FileIO):
self.ptr = file.tell()
indicator = file.read(1)
assert indicator in (OP, ED)
self.key, self.value = load(file)
self.size = file.tell() - self.ptr
示例11: File
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import read [as 别名]
class File(RawIOBase):
'Create a file object wrapping an e[x]ploded zip file'
HEADER = 0
DATA = 1
DESCRIPTOR = 2
DIRECTORY = 3
def __init__(self, path, flags, info, fh=None, base='.', depth=0):
super(File, self).__init__()
self.path = path
self.flags = flags
self.fh = fh
self.info = info
self.depth = depth
self.cursor = 0
self.offset = 0
self.state = File.HEADER
# stream item info
self.stream_offset = 0
self.zip_header = b''
self.descriptor = b''
# data file info
self.data = None
self.data_name = ''
self.data_len = 0
# streams
prefix = os.path.join(base, 'meta', os.path.basename(path))
self.stream = FileIO(prefix + '.stream', 'rb')
self.dir = FileIO(prefix + '.dir', 'rb')
self.data_dir = os.path.join(base, 'data')
# init
self._load_stream_item()
self.lock = threading.Lock()
def _load_stream_item(self):
'Sets the next stream item as current.'
if self.data:
self.data.close()
self.data = None
# open the header so we can know the data file to open, and the
# length of the var fields
raw_header = self.stream.read(STREAM_ITEM.size)
header = StreamItem._make(STREAM_ITEM.unpack(raw_header))
var_fields = header.filename_len + header.extra_field_len
# I would think that b2a_hex should decode the raw bytes...
sha1 = b2a_hex(header.sha).decode('ascii')
# only save the zip part of the header
self.zip_header = (raw_header[:HEADER_DIFF] +
self.stream.read(var_fields))
self.descriptor = self.stream.read(header.descriptor_len)
self.data_name = path.join(*([self.data_dir] +
list(sha1[:self.depth]) + [sha1]))
def _open_data_file(self):
self.data = FileIO(self.data_name, 'rb')
self.data_len = self.data.seek(0, 2)
self.data.seek(0)
def close(self):
self.stream.close()
self.dir.close()
if self.data: self.data.close()
def fileno(self):
return self.fh
def isatty(self):
return False
def read(self, count=-1):
if count < 0: return self.readall()
elif count == 0: return b''
state = self.state
if state == File.HEADER:
previous_offset = self.offset
self.offset += count
result = self.zip_header[previous_offset:self.offset]
self.cursor += len(result)
if self.offset >= len(self.zip_header):
self.state = File.DATA
if not self.data: self._open_data_file()
return result
#.........这里部分代码省略.........