本文整理汇总了Python中io.BufferedWriter方法的典型用法代码示例。如果您正苦于以下问题:Python io.BufferedWriter方法的具体用法?Python io.BufferedWriter怎么用?Python io.BufferedWriter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io
的用法示例。
在下文中一共展示了io.BufferedWriter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_write_exclusive_byte_file
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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
示例2: test_append_byte_file
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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"
示例3: write
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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.
示例4: close
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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
示例5: _wrap_fileobject
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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
示例6: test_write
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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')
示例7: test_write_byte_file
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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
示例8: backport_makefile
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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
示例9: backport_makefile
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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
示例10: backport_makefile
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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
示例11: backport_makefile
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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
示例12: get_or_init_buffered_writer
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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)
示例13: isfileobj
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
def isfileobj(f):
return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))
示例14: _get_text_stdout
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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)
示例15: _get_text_stderr
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
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)