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


Python BZ2File.write方法代码示例

本文整理汇总了Python中bz2.BZ2File.write方法的典型用法代码示例。如果您正苦于以下问题:Python BZ2File.write方法的具体用法?Python BZ2File.write怎么用?Python BZ2File.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bz2.BZ2File的用法示例。


在下文中一共展示了BZ2File.write方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testContextProtocol

# 需要导入模块: from bz2 import BZ2File [as 别名]
# 或者: from bz2.BZ2File import write [as 别名]
def testContextProtocol(self):
        # BZ2File supports the context management protocol
        f = None
        with BZ2File(self.filename, "wb") as f:
            f.write(b"xxx")
        f = BZ2File(self.filename, "rb")
        f.close()
        try:
            with f:
                pass
        except ValueError:
            pass
        else:
            self.fail("__enter__ on a closed file didn't raise an exception")
        try:
            with BZ2File(self.filename, "wb") as f:
                1 // 0
        except ZeroDivisionError:
            pass
        else:
            self.fail("1 // 0 didn't raise an exception") 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:23,代码来源:test_bz2.py

示例2: decompress

# 需要导入模块: from bz2 import BZ2File [as 别名]
# 或者: from bz2.BZ2File import write [as 别名]
def decompress(self, data):
            pop = subprocess.Popen("bunzip2", shell=True,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT)
            pop.stdin.write(data)
            pop.stdin.close()
            ret = pop.stdout.read()
            pop.stdout.close()
            if pop.wait() != 0:
                ret = bz2.decompress(data)
            return ret 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:14,代码来源:test_bz2.py

示例3: createTempFile

# 需要导入模块: from bz2 import BZ2File [as 别名]
# 或者: from bz2.BZ2File import write [as 别名]
def createTempFile(self, crlf=0):
        with open(self.filename, "wb") as f:
            if crlf:
                data = self.DATA_CRLF
            else:
                data = self.DATA
            f.write(data) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:9,代码来源:test_bz2.py

示例4: testWrite

# 需要导入模块: from bz2 import BZ2File [as 别名]
# 或者: from bz2.BZ2File import write [as 别名]
def testWrite(self):
        # "Test BZ2File.write()"
        with BZ2File(self.filename, "w") as bz2f:
            self.assertRaises(TypeError, bz2f.write)
            bz2f.write(self.TEXT)
        with open(self.filename, 'rb') as f:
            self.assertEqual(self.decompress(f.read()), self.TEXT) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:9,代码来源:test_bz2.py

示例5: testWriteChunks10

# 需要导入模块: from bz2 import BZ2File [as 别名]
# 或者: from bz2.BZ2File import write [as 别名]
def testWriteChunks10(self):
        # "Test BZ2File.write() with chunks of 10 bytes"
        with BZ2File(self.filename, "w") as bz2f:
            n = 0
            while 1:
                str = self.TEXT[n*10:(n+1)*10]
                if not str:
                    break
                bz2f.write(str)
                n += 1
        with open(self.filename, 'rb') as f:
            self.assertEqual(self.decompress(f.read()), self.TEXT) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:14,代码来源:test_bz2.py

示例6: testWriteMethodsOnReadOnlyFile

# 需要导入模块: from bz2 import BZ2File [as 别名]
# 或者: from bz2.BZ2File import write [as 别名]
def testWriteMethodsOnReadOnlyFile(self):
        with BZ2File(self.filename, "w") as bz2f:
            bz2f.write("abc")

        with BZ2File(self.filename, "r") as bz2f:
            self.assertRaises(IOError, bz2f.write, "a")
            self.assertRaises(IOError, bz2f.writelines, ["a"]) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:9,代码来源:test_bz2.py

示例7: testBug1191043

# 需要导入模块: from bz2 import BZ2File [as 别名]
# 或者: from bz2.BZ2File import write [as 别名]
def testBug1191043(self):
        # readlines() for files containing no newline
        data = 'BZh91AY&SY\xd9b\x89]\x00\x00\x00\x03\x80\x04\x00\x02\x00\x0c\x00 \x00!\x9ah3M\x13<]\xc9\x14\xe1BCe\x8a%t'
        with open(self.filename, "wb") as f:
            f.write(data)
        with BZ2File(self.filename) as bz2f:
            lines = bz2f.readlines()
        self.assertEqual(lines, ['Test'])
        with BZ2File(self.filename) as bz2f:
            xlines = list(bz2f.readlines())
        self.assertEqual(xlines, ['Test']) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:13,代码来源:test_bz2.py

示例8: testThreading

# 需要导入模块: from bz2 import BZ2File [as 别名]
# 或者: from bz2.BZ2File import write [as 别名]
def testThreading(self):
        # Using a BZ2File from several threads doesn't deadlock (issue #7205).
        data = "1" * 2**20
        nthreads = 10
        with bz2.BZ2File(self.filename, 'wb') as f:
            def comp():
                for i in range(5):
                    f.write(data)
            threads = [threading.Thread(target=comp) for i in range(nthreads)]
            with support.start_threads(threads):
                pass 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:13,代码来源:test_bz2.py

示例9: testMixedIterationReads

# 需要导入模块: from bz2 import BZ2File [as 别名]
# 或者: from bz2.BZ2File import write [as 别名]
def testMixedIterationReads(self):
        # Issue #8397: mixed iteration and reads should be forbidden.
        with bz2.BZ2File(self.filename, 'wb') as f:
            # The internal buffer size is hard-wired to 8192 bytes, we must
            # write out more than that for the test to stop half through
            # the buffer.
            f.write(self.TEXT * 100)
        with bz2.BZ2File(self.filename, 'rb') as f:
            next(f)
            self.assertRaises(ValueError, f.read)
            self.assertRaises(ValueError, f.readline)
            self.assertRaises(ValueError, f.readlines) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:14,代码来源:test_bz2.py

示例10: testThreading

# 需要导入模块: from bz2 import BZ2File [as 别名]
# 或者: from bz2.BZ2File import write [as 别名]
def testThreading(self):
        # Using a BZ2File from several threads doesn't deadlock (issue #7205).
        data = "1" * 2**20
        nthreads = 10
        with bz2.BZ2File(self.filename, 'wb') as f:
            def comp():
                for i in range(5):
                    f.write(data)
            threads = [threading.Thread(target=comp) for i in range(nthreads)]
            for t in threads:
                t.start()
            for t in threads:
                t.join() 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:15,代码来源:test_bz2.py


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