def _runTest(self, text): input_stream = InputStream(text) parser = CUParser(input_stream, unittest.TestCase.id) cu = parser.parse() if len(cu.markers) > 0: print("Test Failed:") in_reader = StringIO(text) i = 1 while True: line = in_reader.readline() if line == "": break line = line[:-1] print("%3d: %s" % (i, line)) i += 1 self.assertEqual(len(cu.markers), 0, "Syntax Errors") # Now, run the linker... v = LinkVisitor([cu]) v.link() # Run the link checker to ensure we didn't miss resolving any v = TestLinker.LinkCheckVisitor() cu.accept(v)
def _runTest(self, text, name): input_stream = InputStream(text) parser = CUParser(input_stream, name) cu = parser.parse() if len(cu.markers) > 0: print("Test Failed:") in_reader = StringIO(text) i=1 while True: line = in_reader.readline() if line == "": break line = line[:-1] print("%3d: %s" % (i, line)) i+=1 self.assertEqual(len(cu.markers), 0, "Errors")
def test_readline(): sio = StringIO(u'123\n456') assert sio.readline(0) == '' assert sio.readline(2) == '12' assert sio.readline(None) == '3\n' assert sio.readline() == '456'
f.close() #with语句来自动帮我们调用close()方法 with open('C:/Users/bingw/Desktop/mygit/gitTest.txt', 'r') as f: print(f.read()) #使用readlines函数读取文件内容 with open('C:/Users/bingw/Desktop/mygit/gitTest.txt', 'r') as f: for line in f.readlines(): print(line.strip()) #StringIO在内存中读写str,如果要操作二进制数据,就需要使用BytesIO,方法和StringIO类似 s = StringIO() s.write('hello') print(s.getvalue()) #读取StringIO f = StringIO('hello,world') while True: str1 = f.readline() if str1 == '': break print(str1.strip()) #os模块的基本功能 print(os.name) #操作系统类型,如果是posix,说明系统是Linux、Unix或Mac OS X,如果是nt,就是Windows系统 print(os.environ) #查看环境变量 print(os.environ.get('JAVA_HOME')) #获取某个环境变量的值 print(os.path.abspath('.')) #查看当前目录的绝对路径 '''在某个目录下创建一个新目录,首先把新目录的完整路径表示出来; os.path.split()用来拆分路径;os.path.splitext()可以直接让你得到文件扩展名''' print(os.path.join('E:/', 'testdir'))
''' @author: xilh @since: 20200128 ''' from _io import StringIO from demo.tools.tool import pline # 创建对象 f = StringIO() f.write('hello') f.write(' ') f.write('world') ret = f.getvalue() print(ret) pline() f = StringIO("abcdefghijk") print(f.read(2)) print(f.readline())