import fileinput with fileinput.input('a.txt') as f: # , backup=".bak", inplace=1 backup inplace 同时有参数才会备份 print("*****访问一个文件 'a.txt'****") for line in f: print(line.strip()) # print(line.strip(), ' # ', f.lineno()) # f.close() with fileinput.input(('a.txt', 'b.txt')) as f: print("---访问两个文件 'a.txt', 'b.txt' ---") for line in f: print(f.lineno(), '.', f.filename(), '--->', f.filelineno(), line.strip())
标签:strip,fileinput,---,简单,print,操作,line,txt From: https://www.cnblogs.com/sangern/p/17493718.html