def test_state_is_None(self): """Tests fileinput.isstdin() 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.isstdin() self.assertEqual(("no active input()", ), cm.exception.args) self.assertIsNone(fileinput._state)
def test_state_is_None(self): """Tests fileinput.isstdin() 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.isstdin() self.assertEqual(("no active input()",), cm.exception.args) self.assertIsNone(fileinput._state)
def clean_clutter_in(files, tabsize=8): if not files: return n_empty = 0 for fname in files: if not os.path.isdir(fname): for line in input(fname, inplace=1): if (isfirstline()): if (not isstdin()): print >> sys.__stdout__, filename() + ':' n_empty = 0 clean_line = line.expandtabs(tabsize).rstrip() if (len(clean_line) == 0): n_empty += 1 else: for i in xrange(n_empty): sys.stdout.write("\n") n_empty = 0 sys.stdout.write(clean_line) sys.stdout.write("\n") # explicitly convert Windows linebreaks into Unix linebreaks wfile = open(fname, "r") wstr = wfile.read() wfile.close() ustr = wstr.replace("\r\n", "\n") ufile = open(fname, 'wb') ufile.write(ustr) ufile.close()
def clean_clutter_in(files, tabsize=8): if not files: return n_empty = 0 for fname in files: if not os.path.isdir(fname): for line in input(fname, inplace=1): if (isfirstline()): if (not isstdin()): print >> sys.__stdout__, filename() + ':' n_empty = 0 clean_line = line.expandtabs(tabsize).rstrip() if (len(clean_line) == 0): n_empty += 1 else: for i in xrange(n_empty): sys.stdout.write("\n") n_empty = 0 sys.stdout.write(clean_line) sys.stdout.write("\n") # explicitly convert Windows linebreaks into Unix linebreaks wfile = open(fname,"r") wstr=wfile.read() wfile.close() ustr = wstr.replace("\r\n", "\n") ufile=open(fname,'wb') ufile.write(ustr) ufile.close()
def main(): p = optparse.OptionParser(__doc__) p.add_option("-n", dest="n", help="number of qual lines to test default:-1" " means test until end of file or until it it possible to " " determine a single file-type", type='int', default=-1) opts, args = p.parse_args() if len(args) > 1: print("Only a single input file is supported.", file=sys.stderr) sys.exit(1) gmin = 99 gmax = 0 valid = [] err_exit = False input_file = fileinput.input(args, openhook=fileinput.hook_compressed) for i, line in enumerate(input_file): if i == 0: input_filename_for_disp = fileinput.filename() if fileinput.isstdin(): input_filename_for_disp = 'STDIN' print("# reading qualities from " "{}".format(input_filename_for_disp), file=sys.stderr) lmin, lmax, qual_val_counts = get_qual_range(line.rstrip()) if lmin < gmin or lmax > gmax: gmin, gmax = min(lmin, gmin), max(lmax, gmax) valid = get_encodings_in_range(gmin, gmax) valid = heuristic_filter(valid, qual_val_counts) if len(valid) == 0: print("no encodings for range: " "{}".format((gmin, gmax)), file=sys.stderr) err_exit = True break if len(valid) == 1 and opts.n == -1: # parsed entire file and found unique guess break if opts.n > 0 and i > opts.n: # parsed up to specified portion; return current guess(es) break input_file.close() if err_exit: sys.exit(1) else: print("{}\t{}\t{}".format(",".join(valid), gmin, gmax))
def main(): p = optparse.OptionParser(__doc__) p.add_option("-n", dest="n", help="number of qual lines to test default:-1" " means test until end of file or until it it possible to " " determine a single file-type", type='int', default=-1) opts, args = p.parse_args() if len(args) > 1: print("Only a single input file is supported.", file=sys.stderr) sys.exit(1) gmin = 99 gmax = 0 valid = [] err_exit = False input_file = fileinput.input(args, openhook=fileinput.hook_compressed) for i, line in enumerate(input_file): if i == 0: input_filename_for_disp = fileinput.filename() if fileinput.isstdin(): input_filename_for_disp = 'STDIN' print("# reading qualities from " "{}".format(input_filename_for_disp), file=sys.stderr) lmin, lmax = get_qual_range(line.rstrip()) if lmin < gmin or lmax > gmax: gmin, gmax = min(lmin, gmin), max(lmax, gmax) valid = get_encodings_in_range(gmin, gmax) if len(valid) == 0: print("no encodings for range: " "{}".format((gmin, gmax)), file=sys.stderr) err_exit = True break if len(valid) == 1 and opts.n == -1: # parsed entire file and found unique guess break if opts.n > 0 and i > opts.n: # parsed up to specified portion; return current guess(es) break input_file.close() if err_exit: sys.exit(1) else: print("{}\t{}\t{}".format(",".join(valid), gmin, gmax))
def file_input(): """ descript: 标准输入的方法二,此方法调用时可以传入多个文件 """ for line in fileinput.input(): meta = [fileinput.filename(), fileinput.fileno(), fileinput.filelineno(), fileinput.isfirstline(), fileinput.isstdin()] print(*meta, end='') print(line, end='')
def main(args): ap = argparse.ArgumentParser() ap.add_argument('pattern', help='the pattern to match') ap.add_argument('files', nargs='*', help='files to be searched') ap.add_argument('-i', '--ignore-case', action='store_true', help='ignore case while searching') ap.add_argument('-v', '--invert', action='store_true', help='invert the search result') ns = ap.parse_args(args) flags = 0 if ns.ignore_case: flags |= re.IGNORECASE pattern = re.compile(ns.pattern, flags=flags) if ns.invert: def fn_predicate(line, newline): return line == newline else: def fn_predicate(line, newline): return line != newline # Do not try to grep directories files = [f for f in ns.files if not os.path.isdir(f)] fileinput.close() # in case it is not closed try: for line in fileinput.input(files): newline = re.sub(pattern, lambda m: _stash.text_color(m.group(), 'red'), line) if fn_predicate(line, newline): if fileinput.isstdin(): fmt = u'{lineno}: {line}' else: fmt = u'{filename}: {lineno}: {line}' print( fmt.format(filename=fileinput.filename(), lineno=fileinput.filelineno(), line=newline.rstrip())) except Exception as err: print("grep: {}: {!s}".format(type(err).__name__, err), file=sys.stderr) finally: fileinput.close()
def test_state_is_not_None(self): """Tests fileinput.isstdin() when fileinput._state is not None. Ensure that it invokes fileinput._state.isstdin() exactly once, returns whatever it returns, and does not modify fileinput._state to point to a different object.""" isstdin_retval = object() instance = MockFileInput() instance.return_values["isstdin"] = isstdin_retval fileinput._state = instance retval = fileinput.isstdin() self.assertExactlyOneInvocation(instance, "isstdin") self.assertIs(retval, isstdin_retval)
def main(args): global _stash ap = argparse.ArgumentParser() ap.add_argument('pattern', help='the pattern to match') ap.add_argument('files', nargs='*', help='files to be searched') ap.add_argument('-i', '--ignore-case', action='store_true', help='ignore case while searching') ap.add_argument('-v', '--invert', action='store_true', help='invert the search result') ap.add_argument('-c', '--count', action='store_true', help='count the search results instead of normal output') ns = ap.parse_args(args) flags = 0 if ns.ignore_case: flags |= re.IGNORECASE pattern = re.compile(ns.pattern, flags=flags) # Do not try to grep directories files = [f for f in ns.files if not os.path.isdir(f)] fileinput.close() # in case it is not closed try: counts = collections.defaultdict(int) for line in fileinput.input(files, openhook=fileinput.hook_encoded("utf-8")): if bool(pattern.search(line)) != ns.invert: if ns.count: counts[fileinput.filename()] += 1 else: if ns.invert: # optimize: if ns.invert, then no match, so no highlight color needed newline = line else: newline = re.sub(pattern, lambda m: _stash.text_color(m.group(), 'red'), line) if fileinput.isstdin(): fmt = u'{lineno}: {line}' else: fmt = u'{filename}: {lineno}: {line}' print(fmt.format(filename=fileinput.filename(), lineno=fileinput.filelineno(), line=newline.rstrip())) if ns.count: for filename, count in counts.items(): fmt = u'{count:6} {filename}' print(fmt.format(filename=filename, count=count)) except Exception as err: print("grep: {}: {!s}".format(type(err).__name__, err), file=sys.stderr) finally: fileinput.close()
def grep(expr, files): try: for line in fileinput.input(files, mode='rU'): line = line.rstrip('\n') if re.search(expr, line): if fileinput.isstdin(): file_name = 'stdin' else: file_name = fileinput.filename() print('{}: {}'.format(file_name, line)) except IOError: print('Error opening file \'{}\'.'.format(fileinput.filename()), file=sys.stderr)
def test_state_is_not_None(self): """Tests fileinput.isstdin() when fileinput._state is not None. Ensure that it invokes fileinput._state.isstdin() exactly once, returns whatever it returns, and does not modify fileinput._state to point to a different object.""" isstdin_retval = object() instance = MockFileInput() instance.return_values["isstdin"] = isstdin_retval fileinput._state = instance retval = fileinput.isstdin() self.assertExactlyOneInvocation(instance, "isstdin") self.assertIs(retval, isstdin_retval) self.assertIs(fileinput._state, instance)
def main(bus, process, name_for_stdin, results_filter, files): if name_for_stdin is None and '-' in files: return False for line in fileinput.input(files): parsed_line = line.strip().split(';') if parsed_line[-1] not in results_filter: continue process( bus, name_for_stdin if fileinput.isstdin() else os.path.basename(fileinput.filename()), parsed_line[0], parsed_line[1], parsed_line[2], [parsed_line[i:i + 2] for i in range(3, len(parsed_line) - 1, 2)]) return True
def bed_to_gff(args, source, field_type): """ Convert input filename from BED to GFF. """ if field_type is None: field_type = "exon" for line in fileinput.input(args): line = line.strip() if source is None: if fileinput.isstdin(): source = "stdin" else: source = os.path.basename(fileinput.filename()) output_line = bed_to_gff_line(line, source, field_type) sys.stdout.write("%s\n" %(output_line))
def clean_clutter_in(files, tabsize=8): if not files: return n_empty = 0 for line in input([ f for f in files if not os.path.isdir(f) ], inplace=1): if (isfirstline()): if (not isstdin()): print >> sys.__stdout__, filename() + ':' n_empty = 0 clean_line = line.expandtabs(tabsize).rstrip() if (len(clean_line) == 0): n_empty += 1 else: for i in xrange(n_empty): sys.stdout.write("\n") n_empty = 0 sys.stdout.write(clean_line) sys.stdout.write("\n")
def clean_clutter_in(files, tabsize=8): if not files: return n_empty = 0 for line in input([f for f in files if not os.path.isdir(f)], inplace=1): if isfirstline(): if not isstdin(): print >> sys.__stdout__, filename() + ":" n_empty = 0 clean_line = line.expandtabs(tabsize).rstrip() if len(clean_line) == 0: n_empty += 1 else: for i in xrange(n_empty): sys.stdout.write("\n") n_empty = 0 sys.stdout.write(clean_line) sys.stdout.write("\n")
def main(args): ap = argparse.ArgumentParser() ap.add_argument('pattern', help='the pattern to match') ap.add_argument('files', nargs='*', help='files to be searched') ap.add_argument('-i', '--ignore-case', action='store_true', help='ignore case while searching') ap.add_argument('-v', '--invert', action='store_true', help='invert the search result') ns = ap.parse_args(args) flags = 0 if ns.ignore_case: flags |= re.IGNORECASE pattern = re.compile(ns.pattern, flags=flags) if ns.invert: def fn_predicate(line, newline): return line == newline else: def fn_predicate(line, newline): return line != newline # Do not try to grep directories files = [f for f in ns.files if not os.path.isdir(f)] fileinput.close() # in case it is not closed try: for line in fileinput.input(files): newline = re.sub(pattern, lambda m: _stash.text_color(m.group(), 'red'), line) if fn_predicate(line, newline): if fileinput.isstdin(): fmt = u'{lineno}: {line}' else: fmt = u'{filename}: {lineno}: {line}' print(fmt.format(filename=fileinput.filename(), lineno=fileinput.filelineno(), line=newline.rstrip())) except Exception as err: print("grep: {}: {!s}".format(type(err).__name__, err), file=sys.stderr) finally: fileinput.close()
def input_files(): """ Return a list containing tuples such that return[0] is the name of the file and return[1] is a slurped string of the file's contents. # TODO: This function doesn't work if there were options passed. In fact, it attempts to open option strings as filenames. Option strings could be removed from `sys.argv`, I guess. This function hangs if it doesn't get input. Press Ctrl-D (EOF) to continue. """ OPT_STRS = list() for item in PARSER_ARGUMENTS: OPT_STRS += item[0] i = 1 while i < len(sys.argv): s = sys.argv[i].split('=')[0].strip() if s in OPT_STRS: t = sys.argv.pop(i) # if s in {"-c","--config","--logfile","--tempdir","-i","--input","-o","--output"}: # t = sys.argv.pop(i) else: i+=1 files = [] s = '' for line in fileinput.input(): if fileinput.isstdin(): s += line elif fileinput.isfirstline(): fname = fileinput.filename() with open(fname) as f: files.append((fname, f.read())) fileinput.nextfile() if s: files.append((None, s)) return files
# webbrowser模块 import webbrowser # webbrowser.open("https://github.com") # fileinput模块 import fileinput # 可以读取参数传入文件,将每一行作为一个迭代器,可以用for来迭代 # 如python some.py f1.txt f2.txt print fileinput.input() fileinput.filename() fileinput.lineno() fileinput.filelineno() fileinput.isfirstline() fileinput.isstdin() fileinput.nextfile() # fileinput.close() # set模块 print set(range(10)) # set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # set取唯一的值 print set([0, 1, 2, 3, 0, 3]) # set([0, 1, 2, 3]) a = set([1, 2, 3]) b = set([2, 3, 4]) print a.union(b) # set([1, 2, 3, 4]) print a | b # set([1, 2, 3, 4]) __or__实现 c = a & b # __and__实现 print c # set([2, 3]) print c.issubset(a) # True print c <= a # True __le__实现
#-*- coding: UTF-8 -*- __author__ = 'mcxiaoke' import os,fileinput # 内部使用FileInput类实现 # fileinput 是一个从标准输入或者文件按行读入的工具类,默认是文本模式,一般的用法是 for line in fileinput.input(['file_input.py']): print fileinput.filename() # 文件名 print fileinput.fileno() #文件描述符,int print fileinput.lineno() #总的行号 print fileinput.filelineno() #当前文件的行号 print fileinput.isstdin() # 是否标准输入 print fileinput.isfirstline() # 是否是文件的第一行 print fileinput.nextfile() # 关闭当前文件,开始读下一个文件 print fileinput.close() # 关闭输入序列
data = {} # the outputfile, if any outputfile = None # counter to record the order in which variables appear counter = 0 #----------------------------------------------------------------------------- # Parse the input for line in fileinput.input(infiles): # in case we are reading from stdin, we want to copy all the data to a log # file so that it can be further analyzed later on. # in case no outputfile was given as argument, we create a temp file # (this has to be done after reading the first line) if fileinput.isstdin(): if outputfile is None: if args.outputfilename is None: fd, args.outputfilename = tempfile.mkstemp(prefix='diststat-') outputfile = os.fdopen(fd, 'w') else: outputfile = open(args.outputfilename, 'w') if args.skip_first and fileinput.isfirstline(): seen_count = {} # if we are reading from stdin, then copy to stdout and the outputfile if fileinput.isstdin(): sys.stdout.write(line) # if we are logging to a file
#!/usr/bin/python from __future__ import print_function import fileinput for line in fileinput.input(): meta = [ fileinput.filename(), fileinput.fileno(), fileinput.filelineno(), fileinput.isfirstline(), fileinput.isstdin() ] print(*meta, end="") print(line, end="")
#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Simplistic grep implementation """ # end_pymotw_header import fileinput import re import sys pattern = re.compile(sys.argv[1]) for line in fileinput.input(sys.argv[2:]): if pattern.search(line): if fileinput.isstdin(): fmt = "{lineno}:{line}" else: fmt = "{filename}:{lineno}:{line}" print fmt.format(filename=fileinput.filename(), lineno=fileinput.filelineno(), line=line.rstrip())
def update_lastmod(filepath): """Replace the lastmod YAML property with a current one. If no lastmod property is found, one is added to the end of the YAML frontmatter block.""" global verbose, debug, dryrun, output_filename new_lastmod = get_new_lastmod(get_local_isotime()) in_yaml = False lastmod_found = False filename = None output_buffer = [] with fileinput.input(files=filepath) as f_input: for line in f_input: # save filename if not reading from stdin if filename == None: if not fileinput.isstdin(): filename = fileinput.filename() if verbose or debug: sys.stderr.write("Reading from file: '" + filename + "' ") else: if verbose or debug: sys.stderr.write("Reading from stdin: ") filename = False if debug: sys.stderr.write("\n") if debug: sys.stderr.write("DEBUG: " + repr(line) + "\n") elif verbose: sys.stderr.write(".") # start/end of YAML block detected # in_yaml == None at beginning, True if we are inside a YAML # block. set in_yaml to False when we exit the YAML block. # if at end of YAML block and no lastmod was modified, we insert # a lastmod before the end of the to YAML block if line.startswith("---"): if in_yaml: in_yaml = False if not lastmod_found: output(new_lastmod, output_buffer) else: in_yaml = True # change an existing lastmod line, don't pass original line elif in_yaml and line.startswith("lastmod"): lastmod_found = True output(new_lastmod, output_buffer) continue # pass original line output(line, output_buffer) # if a file was modified. i.e. filename is a string, not False/None if filename and verbose: sys.stderr.write("\nRead " + str(len(output_buffer)) + " lines from file '" + filename + "'.\n") elif verbose: sys.stderr.write("\nRead " + str(len(output_buffer)) + " lines from stdin.\n") # if no filename was recieved via the flags -o or --output # filename will be False is we are reading from stdin if output_filename == "": output_filename = filename # declared for future use. will remain None, or be either a file or stdout save_file = None # output_filename found if output_filename: # got an output_filename but dryrun if dryrun: if verbose: sys.stderr.write("Dryrun: Not writing to '" + output_filename + "'\n") # no save_file needs to be opened save_file = None # not a dryrun, so we open the file to save to else: if verbose: sys.stderr.write("Saving to '" + output_filename + "\n") save_file = open(output_filename, 'w') # file to save to, so we output to stdout else: save_file = sys.stdout if not dryrun: for line in output_buffer: save_file.write(line) if not (save_file is sys.stdout): save_file.close()
def main(): for line in fileinput.input(): if (fileinput.isfirstline() or fileinput.isstdin()): st = line print(st)
import fileinput import sys for line in fileinput.input(sys.stdin.seek(0)): print(fileinput.isstdin()) print(line.strip('\n'))
def isstdin(file): "checks whether last line was from sys.stdin" return fileinput.isstdin()
def main(): """ main program """ #TODO: extend usage examples usage = """%prog [options] [files] %prog -r 1 -o gist.txt %prog -r 737db156ce0ac2388902 %prog models.py tests.py %prog -p secret.txt""" option_list = [ make_option('-d', '--debug', action='store_true', help='Enable debug mode', dest='debug', default=False), make_option('-r', '--read', action='store', help='Download gist', metavar='id', dest='gistread', type='string'), make_option('-o', '--output', action='store', help='Destination file', metavar='file', dest='gistout', type='string'), make_option('-e', '--extension', action='store', help='File extension. Default .txt', metavar='.ext', dest='gistext', type='string'), make_option('-p', '--private', action='store_true', help='Publish private gist', dest='private', default=False), ] parser = OptionParser(usage, option_list=option_list, version=__version__) options, args = parser.parse_args() if options.debug: logging.basicConfig(level=logging.DEBUG) # parse input #TODO: allow username token by command line? username, token = github_user_token() logging.debug('options: %s', options) logging.debug('args: %s', args) logging.debug('username: %s', username) logging.debug('token: %s', token) # Read gist if options.gistread is not None: if len(args) > 0: parser.error('-r does not allow extra arguments') elif not re.match(r'^\w+$', options.gistread): parser.error('-r invalid gist ID') else: gist_url = gist_txt_url(options.gistread) logging.debug('fetching %s', gist_url) # TODO: git clone option if options.gistout: if os.path.exists(options.gistout): parser.error('-o file exists') out = file(options.gistout, 'w') else: out = sys.stdout count = 0 # TODO: maybe handle multiple files in gist for line in urllib2.urlopen(gist_url): out.write(line) count += 1 if options.gistout: out.close() logging.debug('%d lines written', count) # TODO: better way to known if gist exists if count == 0: main_msg('Unknown Gist or has been deleted', error=True) # Post gist else: # gistfile key generator gistfile_key = ('gistfile'+str(i) for i in itertools.count(1)) default_ext = '.txt' if options.gistext: override_ext = options.gistext else: override_ext = None # post data with login & token postdata = dict(login=username, token=token) # post initial values if options.private: postdata['private'] = 'on' logging.debug('Private gist') # loop over all files for line in fileinput.input(args): ## setup per-file values if fileinput.isfirstline(): gist_current = gistfile_key.next() file_name = 'file_name[%s]' % gist_current file_ext = 'file_ext[%s]' % gist_current file_contents = 'file_contents[%s]' % gist_current if fileinput.isstdin(): # standard input logging.debug('Processing STDIN') postdata[file_name] = '' postdata[file_contents] = '' if override_ext: postdata[file_ext] = override_ext else: postdata[file_ext] = default_ext else: # real file name, ext = os.path.splitext(fileinput.filename()) logging.debug('Processing %s%s', name, ext) postdata[file_name] = name + ext postdata[file_contents] = '' if override_ext: postdata[file_ext] = override_ext elif ext is not None: postdata[file_ext] = ext else: postdata[file_ext] = default_ext # append line to current content postdata[file_contents] += line logging.debug('Post data\n%s', postdata) # do the post url = GIST_URL + 'gists' data = urllib.urlencode(postdata) request = urllib2.Request(url, data) logging.debug('Request: %s', request) # TODO: handle response errors response = urllib2.urlopen(request) logging.debug('Response: %s', response) main_msg('Here is your gist: %s' % response.geturl()) # finish program main_exit()
def update_event(self, inp=-1): self.set_output_val(0, fileinput.isstdin())
def main(args): global _stash ap = argparse.ArgumentParser() ap.add_argument('pattern', help='the pattern to match') ap.add_argument('files', nargs='*', help='files to be searched') ap.add_argument('-i', '--ignore-case', action='store_true', help='ignore case while searching') ap.add_argument('-v', '--invert', action='store_true', help='invert the search result') ap.add_argument('-c', '--count', action='store_true', help='count the search results instead of normal output') ns = ap.parse_args(args) flags = 0 if ns.ignore_case: flags |= re.IGNORECASE pattern = re.compile(ns.pattern, flags=flags) # Do not try to grep directories files = [f for f in ns.files if not os.path.isdir(f)] fileinput.close() # in case it is not closed try: counts = collections.defaultdict(int) for line in fileinput.input(files, openhook=fileinput.hook_encoded("utf-8")): if bool(pattern.search(line)) != ns.invert: if ns.count: counts[fileinput.filename()] += 1 else: if ns.invert: # optimize: if ns.invert, then no match, so no highlight color needed newline = line else: newline = re.sub( pattern, lambda m: _stash.text_color(m.group(), 'red'), line) if fileinput.isstdin(): fmt = u'{lineno}: {line}' else: fmt = u'{filename}: {lineno}: {line}' print( fmt.format(filename=fileinput.filename(), lineno=fileinput.filelineno(), line=newline.rstrip())) if ns.count: for filename, count in counts.items(): fmt = u'{count:6} {filename}' print(fmt.format(filename=filename, count=count)) except Exception as err: print("grep: {}: {!s}".format(type(err).__name__, err), file=sys.stderr) finally: fileinput.close()
#! /usr/bin/env python import sys from fileinput import input, isfirstline, filename, isstdin from string import expandtabs, rstrip for line in input(inplace=1): if (not isstdin() and isfirstline()): current_stdout = sys.stdout sys.stdout = sys.__stdout__ print filename() + ':' sys.stdout = current_stdout print rstrip(expandtabs(line))
import fileinput for line in fileinput.input(): meta = [ fileinput.filename(), fileinput.fileno(), fileinput.filelineno(), fileinput.isfirstline(), fileinput.isstdin()] print(*meta, end=" ") print(line, end=" ")
import fileinput for line in fileinput.input(): meta = ["文件名:" + str(fileinput.filename()), " 文件描述符:" + str(fileinput.fileno()), " 行号:" + str(fileinput.filelineno()), " 首行:" + str(fileinput.isfirstline()), " 标准输入:" + str(fileinput.isstdin()) + " "] meta_ljust = [i.ljust(9) for i in meta] print(*meta_ljust, end="") print(line, end="")
#!/usr/bin/python #-*-coding:UTF-8-*- from __future__ import print_function import sys import fileinput def get_content(): return sys.stdin.readlines() # print(get_content()) for line in fileinput.input(): meta = [fileinput.filename(), fileinput.fileno(), fileinput.filelineno(), \ fileinput.isfirstline(), fileinput.isstdin()] print(*meta, end="") print(line, end="")