Introduction to the fileinput module in Python
The fileinput module can iterate and traverse the content of one or more files. The input () function of this module is somewhat similar to a file.
The difference between the readlines () method is that the former is an iteration object and needs to be iterated using a for loop. The latter reads all rows at a time.
It is very convenient to use fileinput to traverse the file cyclically, format the output, search for and replace the file.
[Typical usage]
import fileinputfor line in fileinput.input(): process(line)
[Basic format]
Fileinput. input ([files [, inplace [, backup [, bufsize [, mode [, openhook])
[Default format]
Fileinput. input (files = None, inplace = False, backup = '', bufsize = 0, mode = 'R', openhook = None)
Files: Specifies the extension of the backup file, such as. bak. If the backup file of this file already exists, it will be overwritten automatically. Bufsize: # buffer size. The default value is 0. If the file is large, you can modify this parameter. The default value is mode: # read/write mode. The default value is read-only openhook: # This hook is used to control all opened files, such as encoding methods;
[Common functions]
Fileinput. input () # Return the object fileinput that can be used for loop traversal. filename () # returns the name of the current file, fileinput. lineno () # returns the number (or serial number) of rows read currently. filelineno () # returns the row number fileinput of the currently read row. isfirstline () # Check whether the current row is the first row of the file fileinput. isstdin () # determine whether the last row reads fileinput from stdin. close () # close the queue
[Common example]
Example 01: Use fileinput to read all rows of a file
>>> Import fileinput >>> for line in fileinput.input('data.txt '): print line, # output result PythonJava C/C ++ ShellCommand Line Method:
#test.pyimport fileinputfor line in fileinput.input(): print fileinput.filename(),'|','Line Number:',fileinput.lineno(),'|: ',linec:>python test.py data.txtdata.txt | Line Number: 1 |: Pythondata.txt | Line Number: 2 |: Javadata.txt | Line Number: 3 |: C/C++data.txt | Line Number: 4 |: Shell
Example 02: LeeUse fileinput to operate on multiple files and modify the content in the same place
# Test. py # --- sample file --- c: Python27> type 1.txt firstsecondc: Python27> type 2.txt thirdfourth # --- sample file --- import fileinputdef process (line): return line. rstrip () + 'line' for line in fileinput.input('1.txt','2.txt '], inplace = 1): print process (line) # --- result output --- c: Python27> type 1.txt first linesecond linec: python27> type 2.txt third linefourth line # --- result output ---Command Line Method:
# Test. pyimport fileinputdef process (line): return line. rstrip () + 'line' for line in fileinput. input (inplace = True): print process (line) # Run the c: Python27> python test. py 1.txt 2.txt
Example 03: Use fileinput to replace the file content and back up the original file
# Sample file: #data.txt PythonJavaC/C ++ Shell # FileName: test. pyimport fileinputfor line in fileinput.input('data.txt ', backup = '. bak ', inplace = 1): print line. rstrip (). replace ('python', 'perl ') # or print line. replace ('python', 'perl '), # final result: Export data.txt PythonJavaC/C ++ Shell # and generate: Export data.txt. bak File
Example 04: convert a CRLF file into an LF file using fileinput
Import fileinputimport sysfor line in fileinput. input (inplace = True): # convert a text file in Windows/DOS format to a Linux file if line [-2:] =: line = line + sys. stdout. write (line)
Example 05: simple File Processing Using fileinput
# FileName: test. pyimport sysimport fileinputfor line in fileinput. input (r 'C: Python27info.txt '): sys. stdout. write ('=>') sys. stdout. write (line) # output result >>=> The Zen of Python, by Tim Peters ==> Beautiful is better than uugly. => Explicit is better than implicit. => Simple is better than complex. => Complex is better than complicated. => Flat is better than nested. => Sparse is better than dense. => Readabi Lity counts. => Special cases Aren' t special enough to break the rules. => Although practicality beats purity. => Errors shocould never pass silently. => Unless explicitly silenced. => In the face of ambiguity, refuse the temptation to guess. => There shoshould be one -- and preferably only one -- obvious way to do it. => Although that way may not be obvious at first unless you're Dutch. => Now is better than ne Ver. => Although never is often better than * right * now. => If the implementation is hard to explain, it's a bad idea. => If the implementation is easy to explain, it may be a good idea. => Namespaces are one honking great idea -- let's do more of those!
Example 06: Use fileinput to process files in batches
# --- Test file: test.txt test1.txt test2.txt test3.txt --- # --- script file: test. py --- import fileinputimport globfor line in fileinput. input (glob. glob (test *. txt): if fileinput. isfirstline (): print '-' * 20, 'reading % s... '% fileinput. filename (), '-' * 20 print str (fileinput. lineno () + ':' + line. upper (), # --- output result: >>> ------------------ Reading test.txt... -------------------- 1: AAAAA2: bbbbbbb3: CCCCC4: DDDDD5: FFFFF ------------------ Reading test1.txt... -------------------- 6: FIRST LINE7: second line -------------------- Reading test2.txt... ------------------ 8: THIRD LINE9: fourth line -------------------- Reading test3.txt... -------------------- 10: this is line 111: this is line 212: this is line 313: this is line 4
Example 07: Use fileinput and re for log analysis: Extract all rows with dates
# -- Sample file -- aaa1970-01-01 13:45:30 Error: *** Due to System Disk spacke not enough... bbb1970-01-02 10:20:30 Error: *** Due to System Out of Memory... ccc # --- test script --- import reimport fileinputimport syspattern = 'd {4}-d {2}-d {2} d {2}: d {2 }: d {2} 'for line in fileinput. input ('error. log', backup = '. bak ', inplace = 1): if re. search (pattern, line): sys. stdout. write (=>) sys. stdout. write (line) # --- test result --- => 13:45:30 Error: ***** Due to System Disk spacke not enough... => 10:20:30 Error: ***** Due to System Out of Memory...
Example 08:Use fileinput and re for analysis: extract qualified phone numbers
# --- Sample file: phone.txt --- 010-110-12345800-333-1234010-9999999905718888888021-88888888-# --- test Script: test. py --- import reimport fileinputpattern = '[010 | 021]-d {8}' # The extracted area code is 010 or 021. Format: 010-12345678for line in fileinput.input('phone.txt '): if re. search (pattern, line): print '=' * 50 print 'Filename:' + fileinput. filename () + '| Line Number:' + str (fileinput. lineno () + '|' + line, # --- output result: --- >>>================================================== ================ Filename: phone.txt | Line Number: 3 | 010-99999999 ======================================== ================== Filename: phone.txt | Line Number: 5 | 021-88888888 >>>
Example 09: Use fileinput to implement functions similar to grep
Import sys
Import re
Import fileinput
Pattern = re. compile (sys. argv [1])
For line in fileinput. input (sys. argv [2]):
If pattern. match (line ):
Print fileinput. filename (), fileinput. filelineno (), line
$./Test. py import. * fileinput *. py
Example 10:Use fileinput for regular expression replacement
# --- Test sample: input.txt * [Learning Python] (# author: Mark Lutz) # --- test Script: test. pyimport fileinputimport re for line in fileinput. input (): line = re. sub (R '*[(. *)] (# (. *) ', R'
', Line. rstrip () print (line) # --- output result: c: Python27> python test. py input.txt Learning Python