当前位置: 首页>>代码示例>>Python>>正文


Python io.BufferedWriter方法代码示例

本文整理汇总了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 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:26,代码来源:test_smbclient_os.py

示例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" 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:21,代码来源:test_smbclient_os.py

示例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. 
开发者ID:flennerhag,项目名称:mlens,代码行数:21,代码来源:numpy_pickle_utils.py

示例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 
开发者ID:CM3D2user,项目名称:Blender-CM3D2-Converter,代码行数:18,代码来源:fileutil.py

示例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 
开发者ID:pfnet,项目名称:pfio,代码行数:22,代码来源:hdfs.py

示例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') 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:22,代码来源:test_zipfile.py

示例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 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:33,代码来源:test_smbclient_os.py

示例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 
开发者ID:danielecook,项目名称:gist-alfred,代码行数:42,代码来源:makefile.py

示例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 
开发者ID:remg427,项目名称:misp42splunk,代码行数:41,代码来源:makefile.py

示例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 
开发者ID:snowflakedb,项目名称:snowflake-connector-python,代码行数:40,代码来源:backport_makefile.py

示例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 
开发者ID:getavalon,项目名称:core,代码行数:42,代码来源:makefile.py

示例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) 
开发者ID:thingsboard,项目名称:thingsboard-gateway,代码行数:10,代码来源:event_storage_writer.py

示例13: isfileobj

# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedWriter [as 别名]
def isfileobj(f):
        return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter)) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:4,代码来源:py3k.py

示例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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:7,代码来源:_winconsole.py

示例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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:7,代码来源:_winconsole.py


注:本文中的io.BufferedWriter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。