我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用fileinput.lineno()。
def test_zero_byte_files(self): t1 = t2 = t3 = t4 = None try: t1 = writeTmp(1, [""]) t2 = writeTmp(2, [""]) t3 = writeTmp(3, ["The only line there is.\n"]) t4 = writeTmp(4, [""]) fi = FileInput(files=(t1, t2, t3, t4)) line = fi.readline() self.assertEqual(line, 'The only line there is.\n') self.assertEqual(fi.lineno(), 1) self.assertEqual(fi.filelineno(), 1) self.assertEqual(fi.filename(), t3) line = fi.readline() self.assertFalse(line) self.assertEqual(fi.lineno(), 1) self.assertEqual(fi.filelineno(), 0) self.assertEqual(fi.filename(), t4) fi.close() finally: remove_tempfiles(t1, t2, t3, t4)
def t02(): """??????????""" for line in fileinput.input('data.txt'): print(fileinput.filename(), '|', 'Line Number:', fileinput.lineno(), '|: ', line)
def t06(): """??fileinput?????""" for line in fileinput.input(glob.glob("d*.txt"), inplace=1): if fileinput.isfirstline(): print('-' * 20, 'Reading %s...' % fileinput.filename(), '-' * 20) print(str(fileinput.lineno()) + ': ' + line.rstrip().upper())
def test_files_that_dont_end_with_newline(self): t1 = t2 = None try: t1 = writeTmp(1, ["A\nB\nC"]) t2 = writeTmp(2, ["D\nE\nF"]) fi = FileInput(files=(t1, t2)) lines = list(fi) self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"]) self.assertEqual(fi.filelineno(), 3) self.assertEqual(fi.lineno(), 6) finally: remove_tempfiles(t1, t2) ## def test_unicode_filenames(self): ## # XXX A unicode string is always returned by writeTmp. ## # So is this needed? ## try: ## t1 = writeTmp(1, ["A\nB"]) ## encoding = sys.getfilesystemencoding() ## if encoding is None: ## encoding = 'ascii' ## fi = FileInput(files=str(t1, encoding)) ## lines = list(fi) ## self.assertEqual(lines, ["A\n", "B"]) ## finally: ## remove_tempfiles(t1)
def test_context_manager(self): try: t1 = writeTmp(1, ["A\nB\nC"]) t2 = writeTmp(2, ["D\nE\nF"]) with FileInput(files=(t1, t2)) as fi: lines = list(fi) self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"]) self.assertEqual(fi.filelineno(), 3) self.assertEqual(fi.lineno(), 6) self.assertEqual(fi._files, ()) finally: remove_tempfiles(t1, t2)
def lineno(self): self.invocation_counts["lineno"] += 1 return self.return_values["lineno"]
def test_state_is_None(self): """Tests fileinput.lineno() when fileinput._state is None. Ensure that it raises RuntimeError with a meaningful error message and does not modify fileinput._state""" fileinput._state = None with self.assertRaises(RuntimeError) as cm: fileinput.lineno() self.assertEqual(("no active input()",), cm.exception.args) self.assertIsNone(fileinput._state)
def countFileLines(path, fileTypes, ignoreDirs=[]): """??????????????? Args: path: ?? fileTypes: ???????????['.py', '.c', '.java'] ignoreDirs: ???????????['.git'] showDetails: ??????????????????? Returns: count: ??? details: {filename: count}???????? """ count = 0 details = {} for root, dirs, filenames in os.walk(path): # print root # ???? # print dirs # ???????? # print filenames # ???????? ignoreFlag = False for eachDir in ignoreDirs: if root.find(eachDir) >= 0: ignoreFlag = True break if ignoreFlag: continue for fn in filenames: typeFlag = False for ft in fileTypes: if fn.endswith(ft): typeFlag = True break if not typeFlag: continue f = os.path.join(root, fn) for each in fileinput.input(f): # print each #??????? pass details[fileinput.filename()] = fileinput.lineno() count += fileinput.lineno() return count, details