Python io.BufferedWriter() Examples
The following are 30
code examples of io.BufferedWriter().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
io
, or try the search function
.

Example #1
Source File: test_zipfile.py From Fluid-Designer with GNU General Public License v3.0 | 7 votes |
def test_write(self): for wrapper in (lambda f: f), Tellable, Unseekable: with self.subTest(wrapper=wrapper): f = io.BytesIO() f.write(b'abc') bf = io.BufferedWriter(f) with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipfp: self.addCleanup(unlink, TESTFN) with open(TESTFN, 'wb') as f2: f2.write(b'111') zipfp.write(TESTFN, 'ones') with open(TESTFN, 'wb') as f2: f2.write(b'222') zipfp.write(TESTFN, 'twos') self.assertEqual(f.getvalue()[:5], b'abcPK') with zipfile.ZipFile(f, mode='r') as zipf: with zipf.open('ones') as zopen: self.assertEqual(zopen.read(), b'111') with zipf.open('twos') as zopen: self.assertEqual(zopen.read(), b'222')
Example #2
Source File: test_smbclient_os.py From smbprotocol with MIT License | 6 votes |
def test_write_exclusive_byte_file(smb_share): file_path = "%s\\%s" % (smb_share, "file.txt") file_contents = b"File Contents\nNewline" with smbclient.open_file(file_path, mode='xb') as fd: assert isinstance(fd, io.BufferedWriter) assert fd.closed is False with pytest.raises(IOError): fd.read() assert fd.tell() == 0 fd.write(file_contents) assert fd.tell() == len(file_contents) assert fd.closed is True with smbclient.open_file(file_path, mode='rb') as fd: assert fd.read() == file_contents with pytest.raises(OSError, match=re.escape("[NtStatus 0xc0000035] File exists: ")): smbclient.open_file(file_path, mode='xb') assert fd.closed is True
Example #3
Source File: test_smbclient_os.py From smbprotocol with MIT License | 6 votes |
def test_append_byte_file(smb_share): file_path = "%s\\%s" % (smb_share, "file.txt") with smbclient.open_file(file_path, mode='ab') as fd: assert isinstance(fd, io.BufferedWriter) with pytest.raises(IOError): fd.read() fd.write(b"abc") assert fd.tell() == 3 with smbclient.open_file(file_path, mode='ab') as fd: assert fd.tell() == 3 fd.write(b"def") assert fd.tell() == 6 with smbclient.open_file(file_path, mode='rb') as fd: assert fd.read() == b"abcdef"
Example #4
Source File: numpy_pickle_utils.py From mlens with MIT License | 6 votes |
def write(self, data): """Write a byte string to the file. Returns the number of uncompressed bytes written, which is always len(data). Note that due to buffering, the file on disk may not reflect the data written until close() is called. """ with self._lock: self._check_can_write() # Convert data type if called by io.BufferedWriter. if isinstance(data, memoryview): data = data.tobytes() compressed = self._compressor.compress(data) self._fp.write(compressed) self._pos += len(data) return len(data) # Rewind the file to the beginning of the data stream.
Example #5
Source File: fileutil.py From Blender-CM3D2-Converter with Apache License 2.0 | 6 votes |
def close(self): """一時ファイルを閉じてリネームします。""" if self.closed: return super(io.BufferedWriter, self).close() self.raw.close() try: if os.path.exists(self.filepath): if self.backup_filepath is not None: shutil.move(self.filepath, self.backup_filepath) else: os.remove(self.filepath) shutil.move(self.temppath, self.filepath) except: os.remove(self.temppath) raise
Example #6
Source File: hdfs.py From pfio with MIT License | 6 votes |
def _wrap_fileobject(self, file_obj: Type['IOBase'], file_path: str, mode: str = 'rb', buffering: int = -1, encoding: Optional[str] = None, errors: Optional[str] = None, newline: Optional[str] = None, closefd: bool = True, opener: Optional[Callable[ [str, int], Any]] = None) -> Type['IOBase']: if 'b' not in mode: file_obj = io.TextIOWrapper(file_obj, encoding, errors, newline) elif 'r' in mode: # Wrapping file_obj with io.BufferedReader to add `peek` support, # which signiciantly improves unpickle performance. file_obj = io.BufferedReader(file_obj) else: file_obj = io.BufferedWriter(file_obj) return file_obj
Example #7
Source File: test_smbclient_os.py From smbprotocol with MIT License | 5 votes |
def test_write_byte_file(smb_share): file_path = "%s\\%s" % (smb_share, "file.txt") file_contents = b"File Contents\nNewline" with smbclient.open_file(file_path, mode='wb') as fd: assert isinstance(fd, io.BufferedWriter) assert fd.closed is False with pytest.raises(IOError): fd.read() assert fd.tell() == 0 fd.write(file_contents) assert fd.tell() == len(file_contents) assert fd.closed is True with smbclient.open_file(file_path, mode='rb') as fd: assert fd.read() == file_contents with smbclient.open_file(file_path, mode='wb') as fd: assert fd.tell() == 0 assert fd.write(b"abc") assert fd.tell() == 3 fd.flush() with smbclient.open_file(file_path, mode='rb') as fd: assert fd.read() == b"abc" # https://github.com/jborean93/smbprotocol/issues/20
Example #8
Source File: makefile.py From gist-alfred with MIT License | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= {"r", "w", "b"}: raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #9
Source File: makefile.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def backport_makefile( self, mode="r", buffering=None, encoding=None, errors=None, newline=None ): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= {"r", "w", "b"}: raise ValueError("invalid mode %r (only r, w, b allowed)" % (mode,)) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #10
Source File: makefile.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def backport_makefile( self, mode="r", buffering=None, encoding=None, errors=None, newline=None ): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= {"r", "w", "b"}: raise ValueError("invalid mode %r (only r, w, b allowed)" % (mode,)) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #11
Source File: backport_makefile.py From snowflake-connector-python with Apache License 2.0 | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """Backport of socket.makefile from Python 3.5.""" if not set(mode) <= {"r", "w", "b"}: raise ValueError( "invalid mode {!r} (only r, w, b allowed)".format(mode) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #12
Source File: makefile.py From core with MIT License | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= set(["r", "w", "b"]): raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #13
Source File: event_storage_writer.py From thingsboard-gateway with Apache License 2.0 | 5 votes |
def get_or_init_buffered_writer(self, file): try: if self.buffered_writer is None or self.buffered_writer.closed: self.buffered_writer = BufferedWriter(FileIO(self.settings.get_data_folder_path() + file, 'a')) return self.buffered_writer except IOError as e: log.error("Failed to initialize buffered writer! Error: %s", e) raise RuntimeError("Failed to initialize buffered writer!", e)
Example #14
Source File: makefile.py From ServerlessCrawler-VancouverRealState with MIT License | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= set(["r", "w", "b"]): raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #15
Source File: makefile.py From ServerlessCrawler-VancouverRealState with MIT License | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= set(["r", "w", "b"]): raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #16
Source File: makefile.py From ServerlessCrawler-VancouverRealState with MIT License | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= set(["r", "w", "b"]): raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #17
Source File: py3k.py From recruit with Apache License 2.0 | 5 votes |
def isfileobj(f): return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
Example #18
Source File: _winconsole.py From recruit with Apache License 2.0 | 5 votes |
def _get_text_stdout(buffer_stream): text_stream = _NonClosingTextIOWrapper( io.BufferedWriter(_WindowsConsoleWriter(STDOUT_HANDLE)), 'utf-16-le', 'strict', line_buffering=True) return ConsoleStream(text_stream, buffer_stream)
Example #19
Source File: _winconsole.py From recruit with Apache License 2.0 | 5 votes |
def _get_text_stderr(buffer_stream): text_stream = _NonClosingTextIOWrapper( io.BufferedWriter(_WindowsConsoleWriter(STDERR_HANDLE)), 'utf-16-le', 'strict', line_buffering=True) return ConsoleStream(text_stream, buffer_stream)
Example #20
Source File: makefile.py From NEIE-Assistant with GNU General Public License v3.0 | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= set(["r", "w", "b"]): raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #21
Source File: py3k.py From lambda-packs with MIT License | 5 votes |
def isfileobj(f): return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
Example #22
Source File: py3k.py From lambda-packs with MIT License | 5 votes |
def isfileobj(f): return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
Example #23
Source File: py3k.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def isfileobj(f): return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
Example #24
Source File: makefile.py From Python24 with MIT License | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= set(["r", "w", "b"]): raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #25
Source File: makefile.py From faces with GNU General Public License v2.0 | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= set(["r", "w", "b"]): raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #26
Source File: makefile.py From splunk-aws-project-trumpet with MIT License | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= set(["r", "w", "b"]): raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #27
Source File: makefile.py From splunk-aws-project-trumpet with MIT License | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= set(["r", "w", "b"]): raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #28
Source File: makefile.py From splunk-aws-project-trumpet with MIT License | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= set(["r", "w", "b"]): raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #29
Source File: makefile.py From vnpy_crypto with MIT License | 5 votes |
def backport_makefile(self, mode="r", buffering=None, encoding=None, errors=None, newline=None): """ Backport of ``socket.makefile`` from Python 3.5. """ if not set(mode) <= set(["r", "w", "b"]): raise ValueError( "invalid mode %r (only r, w, b allowed)" % (mode,) ) writing = "w" in mode reading = "r" in mode or not writing assert reading or writing binary = "b" in mode rawmode = "" if reading: rawmode += "r" if writing: rawmode += "w" raw = SocketIO(self, rawmode) self._makefile_refs += 1 if buffering is None: buffering = -1 if buffering < 0: buffering = io.DEFAULT_BUFFER_SIZE if buffering == 0: if not binary: raise ValueError("unbuffered streams must be binary") return raw if reading and writing: buffer = io.BufferedRWPair(raw, raw, buffering) elif reading: buffer = io.BufferedReader(raw, buffering) else: assert writing buffer = io.BufferedWriter(raw, buffering) if binary: return buffer text = io.TextIOWrapper(buffer, encoding, errors, newline) text.mode = mode return text
Example #30
Source File: py3k.py From vnpy_crypto with MIT License | 5 votes |
def isfileobj(f): return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))