本文整理汇总了Python中io.FileIO.close方法的典型用法代码示例。如果您正苦于以下问题:Python FileIO.close方法的具体用法?Python FileIO.close怎么用?Python FileIO.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.FileIO
的用法示例。
在下文中一共展示了FileIO.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_from_file_memory_duplicate
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [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 close [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: HidrawDS4Device
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [as 别名]
class HidrawDS4Device(DS4Device):
def __init__(self, name, addr, type, hidraw_device, event_device):
try:
self.report_fd = os.open(hidraw_device, os.O_RDWR | os.O_NONBLOCK)
self.fd = FileIO(self.report_fd, "rb+", closefd=False)
self.input_device = InputDevice(event_device)
self.input_device.grab()
except (OSError, IOError) as err:
raise DeviceError(err)
self.buf = bytearray(self.report_size)
super(HidrawDS4Device, self).__init__(name, addr, type)
def read_report(self):
try:
ret = self.fd.readinto(self.buf)
except IOError:
return
# Disconnection
if ret == 0:
return
# Invalid report size or id, just ignore it
if ret < self.report_size or self.buf[0] != self.valid_report_id:
return False
if self.type == "bluetooth":
# Cut off bluetooth data
buf = zero_copy_slice(self.buf, 2)
else:
buf = self.buf
return self.parse_report(buf)
def read_feature_report(self, report_id, size):
op = HIDIOCGFEATURE(size + 1)
buf = bytearray(size + 1)
buf[0] = report_id
return fcntl.ioctl(self.fd, op, bytes(buf))
def write_report(self, report_id, data):
if self.type == "bluetooth":
# TODO: Add a check for a kernel that supports writing
# output reports when such a kernel has been released.
return
hid = bytearray((report_id,))
self.fd.write(hid + data)
def close(self):
try:
self.fd.close()
self.input_device.ungrab()
except IOError:
pass
示例4: close
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [as 别名]
def close(self):
name = self.name
FileIO.close(self)
if self.__temporary:
try:
os.unlink(name)
except Exception as err:
logger.error("Unable to remove %s: %s" % (name, err))
raise(err)
示例5: save_q
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [as 别名]
def save_q(Q, file_location):
"""Saves the current q learning values to the specified location."""
try:
makedirs(dirname(file_location))
except OSError as exc:
pass
file = FileIO(file_location, 'w')
pickle.dump(Q, file)
file.close()
示例6: bench_file_write55
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [as 别名]
def bench_file_write55():
f = FileIO(tmpf.name, "r+")
zblk = b"\x55" * blksize
for i in xrange(filesize // blksize):
pos = 0
while pos < blksize:
n = f.write(memoryview(zblk)[pos:])
assert n != 0
pos += n
f.close()
示例7: get_contents_to_file
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [as 别名]
def get_contents_to_file(self, key, filepath_to_store_to, *, progress_callback=None):
fileobj = FileIO(filepath_to_store_to, mode="wb")
done = False
metadata = {}
try:
metadata = self.get_contents_to_fileobj(key, fileobj, progress_callback=progress_callback)
done = True
finally:
fileobj.close()
if not done:
os.unlink(filepath_to_store_to)
return metadata
示例8: flipBitAt
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [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()
示例9: get_zipdata
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [as 别名]
def get_zipdata(self):
cache_file_name = self.cache_filename
stream = FileIO(cache_file_name, mode='w')
zipfile = ZipFile(stream, 'w')
self.write_zipfile(zipfile)
zipfile.close()
stream.close()
stream = FileIO(cache_file_name, mode='r')
zipdata = stream.readall()
stream.close()
remove(cache_file_name)
return zipdata
示例10: _bench_file_read
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [as 别名]
def _bench_file_read(hasher, expect):
f = FileIO(tmpf.name, "r")
b = bytearray(blksize)
h = hasher()
while 1:
n = f.readinto(b)
if n == 0:
break
h.update(xbuffer(b, 0, n)) # NOTE b[:n] does copy
f.close()
assert h.digest() == expect
示例11: import_users
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [as 别名]
def import_users(self):
"""
Store imported users in a new .htpasswd style file.
"""
log.info(' {} Users...'.format(len(self.jiraData['users'])))
line = '{}:{}\n'
output = FileIO(self.authentication, 'w')
output.write(line.format(self.username, create_hash(self.password)))
for user in self.jiraData['users']:
output.write(line.format(user['name'], user['password']))
output.close()
示例12: createFile
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [as 别名]
def createFile(size):
headers=['Id', 'Name', 'Balance']
try: fp=open('sample.txt', 'w')
except:fp=FileIO('sample.txt','w')
fp.truncate()
table=getTable(size)
for row in table:
i=0
for item in row:
readyItem=headers[i]+':'+item+'\n'
i+=1
fp.write(readyItem)
fp.write('\n')
fp.close()
示例13: get_contents_to_file
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [as 别名]
def get_contents_to_file(self, key, filepath_to_store_to):
key = self.format_key_for_backend(key)
self.log.debug("Starting to fetch the contents of: %r to: %r", key, filepath_to_store_to)
fileobj = FileIO(filepath_to_store_to, mode="wb")
done = False
metadata = {}
try:
metadata = self.get_contents_to_fileobj(key, fileobj)
done = True
finally:
fileobj.close()
if not done:
os.unlink(filepath_to_store_to)
return metadata
示例14: get_contents_to_file
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [as 别名]
def get_contents_to_file(self, obj_key, filepath_to_store_to):
self.log.debug("Starting to fetch the contents of: %r to: %r", obj_key, filepath_to_store_to)
fileobj = FileIO(filepath_to_store_to, mode="wb")
try:
done = False
request = self.gs_objects.get_media(bucket=self.bucket_name, object=obj_key)
download = MediaIoBaseDownload(fileobj, request, chunksize=CHUNK_SIZE)
while not done:
status, done = download.next_chunk()
if status:
self.log.debug("Download of %r to %r: %d%%", obj_key, filepath_to_store_to, status.progress() * 100)
finally:
fileobj.close()
if not done:
os.unlink(filepath_to_store_to)
示例15: FileDataReader
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import close [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))