本文整理匯總了Python中io.FileIO方法的典型用法代碼示例。如果您正苦於以下問題:Python io.FileIO方法的具體用法?Python io.FileIO怎麽用?Python io.FileIO使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io
的用法示例。
在下文中一共展示了io.FileIO方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: download_file
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def download_file(service, file_id, location, filename, mime_type):
if 'vnd.google-apps' in mime_type:
request = service.files().export_media(fileId=file_id,
mimeType='application/pdf')
filename += '.pdf'
else:
request = service.files().get_media(fileId=file_id)
fh = io.FileIO(location + filename, 'wb')
downloader = MediaIoBaseDownload(fh, request, 1024 * 1024 * 1024)
done = False
while done is False:
try:
status, done = downloader.next_chunk()
except:
fh.close()
os.remove(location + filename)
sys.exit(1)
print(f'\rDownload {int(status.progress() * 100)}%.', end='')
sys.stdout.flush()
print('')
示例2: add_log_stream
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def add_log_stream(self, stream=sys.stderr, level=logging.INFO):
"""
Add a stream where messages are outputted to.
@param stream: stderr/stdout or a file stream
@type stream: file | FileIO | StringIO
@param level: minimum level of messages to be logged
@type level: int | long
@return: None
@rtype: None
"""
assert self.is_stream(stream)
# assert isinstance(stream, (file, io.FileIO))
assert level in self._levelNames
err_handler = logging.StreamHandler(stream)
err_handler.setFormatter(self.message_formatter)
err_handler.setLevel(level)
self._logger.addHandler(err_handler)
示例3: __init__
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def __init__(self, default_compression="gz", logfile=None, verbose=True):
"""
Constructor
@attention:
@param default_compression: default compression used for files
@type default_compression: str | unicode
@param logfile: file handler or file path to a log file
@type logfile: file | io.FileIO | StringIO.StringIO | basestring
@param verbose: Not verbose means that only warnings and errors will be past to stream
@type verbose: bool
@return: None
@rtype: None
"""
assert logfile is None or isinstance(logfile, basestring) or self.is_stream(logfile)
assert isinstance(default_compression, basestring), "separator must be string"
assert isinstance(verbose, bool), "verbose must be true or false"
assert default_compression.lower() in self._open, "Unknown compression: '{}'".format(default_compression)
super(Archive, self).__init__(label="Archive", default_compression=default_compression, logfile=logfile, verbose=verbose)
self._open['tar'] = tarfile.open
self._default_compression = default_compression
示例4: __init__
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def __init__(self, separator="\t", logfile=None, verbose=True):
"""
Handle tab separated files
@attention:
@param separator: default character assumed to separate values in a file
@type separator: str | unicode
@param logfile: file handler or file path to a log file
@type logfile: file | io.FileIO | StringIO.StringIO | basestring
@param verbose: Not verbose means that only warnings and errors will be past to stream
@type verbose: bool
@return: None
@rtype: None
"""
assert logfile is None or isinstance(logfile, basestring) or self.is_stream(logfile)
assert isinstance(separator, basestring), "separator must be string"
assert isinstance(verbose, bool), "verbose must be true or false"
super(MetadataTable, self).__init__(label="MetadataReader", logfile=logfile, verbose=verbose)
self._number_of_rows = 0
self._meta_table = {}
self._separator = separator
self._list_of_column_names = []
示例5: add_log_stream
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def add_log_stream(self, stream=sys.stderr, level=logging.INFO):
"""
Add a stream where messages are outputted to.
@param stream: stderr/stdout or a file stream
@type stream: file | FileIO | StringIO
@param level: minimum level of messages to be logged
@type level: int | long
@return: None
@rtype: None
"""
assert self.is_stream(stream)
# assert isinstance(stream, (file, io.FileIO))
assert level in self._levelNames
err_handler = logging.StreamHandler(stream)
err_handler.setFormatter(self.message_formatter)
err_handler.setLevel(level)
self._logger.addHandler(err_handler)
示例6: __connect
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def __connect(self):
device_addr, hidraw_device, event_device = self.__find_device()
if device_addr is None:
return False
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()
buf = bytearray(38)
buf[0] = 0x02
try:
return bool(fcntl.ioctl(self.__fd, 3223734279, bytes(buf)))
except:
pass
if self.recv():
self.update_controller()
示例7: download_file
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def download_file(service, file_id, location, filename, mime_type):
if 'vnd.google-apps' in mime_type:
request = service.files().export_media(fileId=file_id,
mimeType='application/pdf')
filename += '.pdf'
else:
request = service.files().get_media(fileId=file_id)
fh = io.FileIO(location + filename, 'wb')
downloader = MediaIoBaseDownload(fh, request, 1024 * 1024 * 1024)
done = False
while done is False:
try:
status, done = downloader.next_chunk()
except:
fh.close()
os.remove(location + filename)
sys.exit(1)
print '\rDownload {}%.'.format(int(status.progress() * 100)),
sys.stdout.flush()
print ''
示例8: _get_cbar_png_bytes
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def _get_cbar_png_bytes(cmap):
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
image_data = cmap(gradient, bytes=True)
image = Image.fromarray(image_data, 'RGBA')
# ostream = io.FileIO('../cmaps/' + cmap_name + '.png', 'wb')
# image.save(ostream, format='PNG')
# ostream.close()
ostream = io.BytesIO()
image.save(ostream, format='PNG')
cbar_png_bytes = ostream.getvalue()
ostream.close()
cbar_png_data = base64.b64encode(cbar_png_bytes)
cbar_png_bytes = cbar_png_data.decode('unicode_escape')
return cbar_png_bytes
示例9: _log_message
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def _log_message(self, path: str, message: str) -> None:
# We use io.FileIO here because it guarantees that write() is implemented with a single write syscall,
# and on Linux, writes to O_APPEND files with a single write syscall are atomic.
#
# https://docs.python.org/2/library/io.html#io.FileIO
# http://article.gmane.org/gmane.linux.kernel/43445
try:
with io.FileIO(path, mode=self.mode, closefd=True) as f:
with self.maybe_flock(f):
f.write(message.encode("UTF-8"))
except IOError as e:
print(
"Could not log to {}: {}: {} -- would have logged: {}".format(
path, type(e).__name__, str(e), message
),
file=sys.stderr,
)
示例10: create_manifest
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def create_manifest(data_path, tag, ordered=True):
manifest_path = '%s_manifest.csv' % tag
file_paths = []
wav_files = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(data_path)
for f in fnmatch.filter(files, '*.wav')]
size = len(wav_files)
counter = 0
for file_path in wav_files:
file_paths.append(file_path.strip())
counter += 1
update_progress(counter / float(size))
print('\n')
if ordered:
_order_files(file_paths)
counter = 0
with io.FileIO(manifest_path, "w") as f:
for wav_path in file_paths:
transcript_path = wav_path.replace('/wav/', '/txt/').replace('.wav', '.txt')
sample = os.path.abspath(wav_path) + ',' + os.path.abspath(transcript_path) + '\n'
f.write(sample.encode('utf-8'))
counter += 1
update_progress(counter / float(size))
print('\n')
示例11: convert_file
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def convert_file(self, file_id):
# Get file metadata
metadata = service.files().get(fileId=file_id, fields="name").execute()
# Download the file and then call do_upload() on it
request = service.files().get_media(fileId=file_id)
path = "%s/%s" % (get_downloads_folder(), metadata['name'])
fh = io.FileIO(path, "wb")
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
_, done = downloader.next_chunk()
print("Downloaded %s" % metadata['name'])
do_upload(path, service)
# An alternative method would be to use partial download headers
# and convert and upload the parts individually. Perhaps a
# future release will implement this.
# Mode sets the mode of updating 0 > Verbose, 1 > Notification, 2 > silent
示例12: __init__
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def __init__(self, logfile=None, verbose=True, debug=False, seed=None, tmp_dir=None):
"""
Anonymize fasta sequences
@attention: 'shuf' is used which loads everything into memory!
@param logfile: file handler or file path to a log file
@type logfile: file | io.FileIO | StringIO.StringIO | str | unicode
@param verbose: Not verbose means that only warnings and errors will be past to stream
@type verbose: bool
@param debug: more output and files are kept, manual clean up required
@type debug: bool
@param seed: The seed written to the random_source file used by the 'shuf' command
@type seed: long | int | float | str | unicode
@param tmp_dir: directory for temporary files, like the random_source file for 'shuf'
@type tmp_dir: str | unicode
@return: None
@rtype: None
"""
assert isinstance(verbose, bool)
assert isinstance(debug, bool)
assert seed is None or isinstance(seed, (long, int, float, basestring))
assert tmp_dir is None or isinstance(tmp_dir, basestring)
if tmp_dir is not None:
assert self.validate_dir(tmp_dir)
else:
tmp_dir = tempfile.gettempdir()
self._tmp_dir = tmp_dir
super(FastaAnonymizer, self).__init__(logfile, verbose, debug, label="FastaAnonymizer")
if seed is not None:
random.seed(seed)
script_dir = os.path.dirname(self.get_full_path(__file__))
self._anonymizer = os.path.join(script_dir, "anonymizer.py")
self._fastastreamer = os.path.join(script_dir, "fastastreamer.py")
assert self.validate_file(self._anonymizer)
assert self.validate_file(self._fastastreamer)
示例13: stream_directory
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def stream_directory(self, directory, out_stream=sys.stdout, file_format="fastq", extension="fq", paired=False):
"""
Stream sequences consecutively
@attention:
@param directory: A directory
@type directory: basestring
@param out_stream: A stream the output will be written to.
@type out_stream: file | io.FileIO | StringIO.StringIO
@param file_format: Fasta format of input and output. Either 'fasta' or 'fastq'.
@type file_format: basestring
@param extension: file extension to be filtered for
@type extension: basestring
@param paired: sequences are streamed interweaved from a pair of files if True, else consecutively
@type paired: bool
@return: None
@rtype: None
"""
assert isinstance(directory, basestring)
directory = FastaStreamer.get_full_path(directory)
assert self.validate_dir(directory)
assert self.is_stream(out_stream)
assert isinstance(file_format, basestring)
file_format = file_format.lower()
assert file_format in self._legal_formats
assert extension is None or isinstance(extension, basestring)
list_of_file = self.get_files_in_directory(directory, extension=extension)
if not paired:
self.consecutive_stream(list_of_file, out_stream=out_stream, file_format=file_format)
else:
self.interweave_stream(list_of_file, out_stream=out_stream, file_format=file_format, extension=extension)
示例14: stream_file
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def stream_file(self, file_path, out_stream=sys.stdout, file_format="fastq", paired=False):
"""
Stream sequences consecutively
@attention:
@param file_path: A file path
@type file_path: basestring
@param out_stream: A stream the output will be written to.
@type out_stream: file | io.FileIO | StringIO.StringIO
@param file_format: Fasta format of input and output. Either 'fasta' or 'fastq'.
@type file_format: basestring
@param paired: sequences are streamed as pair, else one by one
@type paired: bool
@return: None
@rtype: None
"""
assert isinstance(file_path, basestring)
file_path = FastaStreamer.get_full_path(file_path)
assert self.validate_file(file_path)
assert self.is_stream(out_stream)
assert isinstance(file_format, basestring)
file_format = file_format.lower()
assert file_format in self._legal_formats
self.consecutive_stream(file_path, out_stream=out_stream, file_format=file_format, paired=paired)
示例15: is_stream
# 需要導入模塊: import io [as 別名]
# 或者: from io import FileIO [as 別名]
def is_stream(stream):
return isinstance(stream, (file, io.FileIO, StringIO.StringIO)) or stream.__class__ is StringIO.StringIO