{"id":1171,"date":"2023-07-20T18:32:00","date_gmt":"2023-07-20T13:02:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=1171"},"modified":"2024-03-01T17:11:36","modified_gmt":"2024-03-01T11:41:36","slug":"fileinput-module","status":"publish","type":"post","link":"https:\/\/geekpython.in\/fileinput-module","title":{"rendered":"How to Read Multiple Files Simultaneously With fileinput Module In Python"},"content":{"rendered":"\n<p>The&nbsp;<code>fileinput<\/code>&nbsp;module is a part of the standard library and is used when someone needs to iterate the contents of multiple files simultaneously. Well, Python&#8217;s in-built&nbsp;<code>open()<\/code>&nbsp;function can also be used for iterating the content but for only one file at a time.<\/p>\n\n\n\n<p>You&#8217;ll explore the classes and functions provided by the&nbsp;<code>fileinput<\/code>&nbsp;module to iterate over multiple files.<\/p>\n\n\n\n<p>But one thing, you could use&nbsp;<code>fileinput<\/code>&nbsp;to iterate the single file also, but it would be better to use the&nbsp;<code>open()<\/code>&nbsp;function for it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-basic-usage\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-basic-usage\"><\/a>Basic Usage<\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import fileinput\n\n# Creating fileinput instance and passing multiple files\nstream = fileinput.input(files=('test.txt',\n                                'sample.txt',\n                                'index.html'))\n\n# Iterating the content\nfor data in stream:\n    print(data)<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>fileinput<\/code>&nbsp;module was first imported, and then the&nbsp;<code>fileinput<\/code>&nbsp;instance was created by calling&nbsp;<code>fileinput.input()<\/code>&nbsp;and passing the tuple of files (<code>test.txt<\/code>,&nbsp;<code>sample.txt<\/code>, and&nbsp;<code>index.html<\/code>). This will result in the return of an iterator.<\/p>\n\n\n\n<p>The contents of the files were then iterated and printed using the&nbsp;<code>for<\/code>&nbsp;loop.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Hi, I am a test file.\nHi, I am a sample file for testing.\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;title&gt;Test HTML File&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;Hi, I am a simple HTML File.&lt;\/h1&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>Another approach would be to use the&nbsp;<code>fileinput<\/code>&nbsp;module as a&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/geekpython.in\/context-managers-and-python-with-statement\" target=\"_blank\">context manager<\/a>. This method is somewhat safe because it ensures that the&nbsp;<code>fileinput<\/code>&nbsp;instance is closed even if an exception occurs.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import fileinput\n\nwith fileinput.input(files=('test.txt', 'sample.txt')) as files:\n    for data in files:\n        print(data)<\/pre><\/div>\n\n\n\n<p>In the above demonstration, the&nbsp;<code>fileinput<\/code>&nbsp;module was used as a context manager with the&nbsp;<code>'with'<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>The above code will return an iterator and will assign it to the&nbsp;<code>files<\/code>&nbsp;variable (due to the&nbsp;<code>as<\/code>&nbsp;clause) then the data will be iterated using the&nbsp;<code>files<\/code>&nbsp;variable.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Hi, I am a test file.\nHi, I am a sample file for testing.<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-the-fileinputinput-function\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-the-fileinputinput-function\"><\/a>The fileinput.input() Function<\/h2>\n\n\n\n<p>The&nbsp;<code>fileinput.input()<\/code>&nbsp;function is the primary interface of the&nbsp;<code>fileinput<\/code>&nbsp;module, by using it, the purpose of using the&nbsp;<code>fileinput<\/code>&nbsp;module is nearly fulfilled. You saw a glimpse of the&nbsp;<code>fileinput.input()<\/code>&nbsp;function in the previous section, this time, you&#8217;ll learn more about it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-syntax\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-syntax\"><\/a>Syntax<\/h3>\n\n\n\n<p><code>fileinput.input(files=None, inplace=False, backup='', mode='r', openhook=None, encoding=None, errors=None)<\/code><\/p>\n\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<p><code>files<\/code>: Defaults to&nbsp;<code>None<\/code>. Takes a single file or multiple files to be processed.<\/p>\n\n\n\n<p><code>inplace<\/code>: Defaults to&nbsp;<code>False<\/code>. When set to&nbsp;<code>True<\/code>, the files can be modified directly.<\/p>\n\n\n\n<p><code>backup<\/code>: Defaults to an empty string. The extension is specified for the backup files when&nbsp;<code>inplace<\/code>&nbsp;is set to&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p><code>mode<\/code>: Default to read mode. This can only open files in read mode hence, we can open the file in&nbsp;<code>r<\/code>,&nbsp;<code>rb<\/code>,&nbsp;<code>rU<\/code>, and&nbsp;<code>U<\/code>.<\/p>\n\n\n\n<p><code>openhook<\/code>: Defaults to&nbsp;<code>None<\/code>. A custom function for controlling how files are opened.<\/p>\n\n\n\n<p><code>encoding<\/code>: Defaults to&nbsp;<code>None<\/code>. Specifies the encoding to be used to read the files.<\/p>\n\n\n\n<p><code>errors<\/code>: Defaults to&nbsp;<code>None<\/code>. Specifies how the errors should be handled.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-modifying-the-files-before-reading\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-modifying-the-files-before-reading\"><\/a>Modifying the Files Before Reading<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import fileinput\n\nwith fileinput.input(files=('test.txt', 'sample.txt'), inplace=True) as files:\n    for data in files:\n        modified_content = data.lower()\n        print(modified_content)<\/pre><\/div>\n\n\n\n<p>The parameter&nbsp;<code>inplace<\/code>&nbsp;is set to&nbsp;<code>True<\/code>&nbsp;in the above code, which enables the editing of the file before reading.<\/p>\n\n\n\n<p>The upper code will lowercase the content present inside both files (<code>test.txt<\/code>&nbsp;and&nbsp;<code>sample.txt<\/code>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-storing-backup-of-files\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-storing-backup-of-files\"><\/a>Storing Backup of Files<\/h3>\n\n\n\n<p>When the&nbsp;<code>inplace<\/code>&nbsp;parameter is set to&nbsp;<code>True<\/code>, the original files can be edited, but the original state of the files can be saved in another file using the&nbsp;<code>backup<\/code>&nbsp;parameter.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import fileinput\n\nwith fileinput.input(files=('test.txt', 'sample.txt'),\n                     inplace=True, backup='.bak') as files:\n    for data in files:\n        modified_content = data.capitalize()\n        print(modified_content)<\/pre><\/div>\n\n\n\n<p>The above code will capitalize the content and the original files will be saved as&nbsp;<code>test.txt.bak<\/code>&nbsp;and&nbsp;<code>sample.txt.bak<\/code>&nbsp;due to the&nbsp;<code>backup='.bak'<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-controlling-the-opening-of-the-file\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-controlling-the-opening-of-the-file\"><\/a>Controlling the Opening of the File<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import fileinput\n\ndef custom_open(filename, mode):\n    data = open(filename, \"a+\")\n    data.write(\" Data added through function.\")\n    return open(filename, mode)\n\nwith fileinput.input(files=(\"test.txt\", \"sample.txt\"), openhook=custom_open) as file:\n    for data in file:\n        print(data)<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>custom_open()<\/code>&nbsp;function is defined that takes two parameters&nbsp;<code>filename<\/code>&nbsp;and&nbsp;<code>mode<\/code>. The function opens the file in&nbsp;<code>append + read<\/code>&nbsp;mode and then writes the string and returns the file object.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The hook must be a function that takes two arguments,&nbsp;<code>filename<\/code>&nbsp;and&nbsp;<code>mode<\/code>, and returns an accordingly opened file-like object.<a target=\"_blank\" href=\"https:\/\/docs.python.org\/3\/library\/fileinput.html\" rel=\"noreferrer noopener\">Source<\/a><\/p>\n<\/blockquote>\n\n\n\n<p>The files are then passed to the&nbsp;<code>fileinput.input()<\/code>&nbsp;function, and the&nbsp;<code>openhook<\/code>&nbsp;parameter is set to&nbsp;<code>custom_open<\/code>. The&nbsp;<code>custom_open()<\/code>&nbsp;function will be in charge of opening the files. The file content was iterated and printed.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Hi, i am a test file. Data added through function.\nHi, i am a sample file for testing. Data added through function.<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-reading-unicode-characters\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-reading-unicode-characters\"><\/a>Reading Unicode Characters<\/h3>\n\n\n\n<p>You have a file having Unicode characters and need to read that file, to read Unicode characters, specific encodings are used.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">with fileinput.input(files=('test_unicode.txt'), encoding='utf-8') as files:\n    for data in files:\n        print(data)<\/pre><\/div>\n\n\n\n<p>The UTF-8 encoding can be used to read the Unicode characters, hence, the&nbsp;<code>encoding<\/code>&nbsp;parameter is set to&nbsp;<code>utf-8<\/code>&nbsp;encoding.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">\ud83d\ude01\ud83d\ude02\ud83d\ude05<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-handling-errors\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-handling-errors\"><\/a>Handling Errors<\/h3>\n\n\n\n<p>To handle the error, use the&nbsp;<code>errors<\/code>&nbsp;parameter. Take the above code as an example: if the&nbsp;<code>encoding<\/code>&nbsp;was not specified, the code would throw a&nbsp;<code>UnicodeError<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">with fileinput.input(files=('test_bin.txt'), errors='ignore') as files:\n    for data in files:\n        print(data)\n\n----------\n\u00f0\u0178\u02dc\u00f0\u0178\u02dc\u201a\u00f0\u0178\u02dc\u2026<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>errors<\/code>&nbsp;parameter is set to&nbsp;<code>ignore<\/code>, which means that the error will be ignored. The&nbsp;<code>errors<\/code>&nbsp;parameter can also be set to&nbsp;<code>strict<\/code>&nbsp;(raise an exception if an error occurs) or&nbsp;<code>replace<\/code>&nbsp;(replace an error with a specified error).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-functions-to-access-input-file-information\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-functions-to-access-input-file-information\"><\/a>Functions to Access Input File Information<\/h3>\n\n\n\n<p>There are some functions that can be used to access the information of the input files which are being processed using the&nbsp;<code>fileinput.input()<\/code>&nbsp;function.<\/p>\n\n\n\n<p><strong>Getting the File Names<\/strong><\/p>\n\n\n\n<p>Using the&nbsp;<code>fileinput.filename()<\/code>&nbsp;function, the name of the currently processed files can be displayed.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">with fileinput.input(files=('test.txt', 'sample.txt')) as files:\n    for data in files:\n        print(f\"File: {fileinput.filename()}\")\n        print(data)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">File: test.txt\nHi, i am a test file. Data added through function. Added data to the file.\nFile: sample.txt\nHi, i am a sample file for testing. Data added through function. Added data to the file.<\/pre><\/div>\n\n\n\n<p><strong>Getting the File Descriptor and Line and File Line Number<\/strong><\/p>\n\n\n\n<p>The&nbsp;<code>fileinput.fileno()<\/code>&nbsp;function returns the active file&#8217;s file descriptor, the&nbsp;<code>fileinput.lineno()<\/code>&nbsp;function returns the cumulative line number, and the&nbsp;<code>fileinput.filelineno()<\/code>&nbsp;function returns the line number of the currently processed file.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">with fileinput.input(files=('test.txt', 'sample.txt')) as files:\n    for data in files:\n        print(f\"{fileinput.filename()}'s File Descriptor: {fileinput.fileno()}\")\n        print(f\"{fileinput.filename()}'s File Line Number: {fileinput.filelineno()}\")\n        print(f\"{fileinput.filename()}'s File Cumulative Line No.: {fileinput.lineno()}\")<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">test.txt's File Descriptor: 3\ntest.txt's File Line Number: 1\ntest.txt's File Cumulative Line No.: 1\n\nsample.txt's File Descriptor: 3\nsample.txt's File Line Number: 1\nsample.txt's File Cumulative Line No.: 2<\/pre><\/div>\n\n\n\n<p><strong>Checking Reading Status<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">with fileinput.input(files=('test.txt', 'sample.txt')) as files:\n    for data in files:\n        print(f\"Read First Line: {fileinput.isfirstline()}\")\n        print(f\"Last Line Read From sys.stdin: {fileinput.isstdin()}\")\n\n----------\nRead First Line: True\nLast Line Read From sys.stdin: False\nRead First Line: True\nLast Line Read From sys.stdin: False<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>fileinput.isfirstline()<\/code>&nbsp;function returns&nbsp;<code>True<\/code>&nbsp;if the line read from the current file is the first line otherwise returns&nbsp;<code>False<\/code>, since both files contain a single line, it returned&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p>When the last line of the input file was read from&nbsp;<code>sys.stdin<\/code>, the&nbsp;<code>fileinput.isstdin()<\/code>&nbsp;function returns&nbsp;<code>True<\/code>, otherwise, it returns&nbsp;<code>False<\/code>.<\/p>\n\n\n\n<p><strong>Closing the File<\/strong><\/p>\n\n\n\n<p>When using&nbsp;<code>fileinput.input()<\/code>&nbsp;function as the context manager with the&nbsp;<code>with<\/code>&nbsp;statement, the file closes anyway but&nbsp;<code>fileinput.close()<\/code>&nbsp;function is also used to close the resources when the work is done.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import fileinput\n\nwith fileinput.input(files=('test.txt', 'sample.txt')) as file:\n    for data in file:\n        if data &gt; data[:26]:\n            fileinput.close()\n            print('File has more than 25 characters.')\n        else:\n            print(data)<\/pre><\/div>\n\n\n\n<p>The above code demonstrates the use of the&nbsp;<code>fileinput.close()<\/code>&nbsp;function, which closes the file if it contains more than 25 characters and prints a message otherwise the content is printed.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">File has more than 25 characters.<\/pre><\/div>\n\n\n\n<p>However, because the file contained more than 25 characters, the file was closed and the message was printed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-the-fileinput-class\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-the-fileinput-class\"><\/a>The FileInput&nbsp;<strong>Class<\/strong><\/h2>\n\n\n\n<p>The&nbsp;<code>fileinput.FileInput<\/code>&nbsp;class is an object-oriented alternative to the&nbsp;<code>fileinput.input()<\/code>&nbsp;function. The parameters are identical to those of the&nbsp;<code>input()<\/code>&nbsp;function.<\/p>\n\n\n\n<p><strong>Syntax<\/strong><\/p>\n\n\n\n<p><code>fileinput.FileInput(files=None, inplace=False, backup='', mode='r', openhook=None, encoding=None, errors=None)<\/code><\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import fileinput\n\nclass OpenMultipleFiles:\n    def __init__(self, *args):\n        self.args = args\n\n    def custom_open(self, filename, mode):\n        data = open(filename, \"a+\")\n        data.write(\" Added data to the file.\")\n        return open(filename, mode)\n\n    def read(self):\n        with fileinput.FileInput(files=(self.args), openhook=OpenMultipleFiles().custom_open) as file:\n            for data in file:\n                print(data)\n\nobj = OpenMultipleFiles('test.txt', 'sample.txt')\nobj.read()<\/pre><\/div>\n\n\n\n<p>The class&nbsp;<code>OpenMultipleFiles<\/code>&nbsp;is defined in the above code. The class has an&nbsp;<code>__init__<\/code>&nbsp;method that takes variadic arguments.<\/p>\n\n\n\n<p>A&nbsp;<code>custom_open<\/code>&nbsp;method is defined within the class that opens the file in&nbsp;<code>append+read<\/code>&nbsp;mode, writes some data to the file, and returns the file object.<\/p>\n\n\n\n<p>The&nbsp;<code>read<\/code>&nbsp;method is defined and within the&nbsp;<code>read<\/code>&nbsp;method the instance of the&nbsp;<code>fileinput.FileInput<\/code>&nbsp;is created and passed the&nbsp;<code>self.args<\/code>&nbsp;as the files argument and the&nbsp;<code>openhook<\/code>&nbsp;parameter is set to&nbsp;<code>OpenMultipleFiles().custom_open<\/code>. The contents of the files are then iterated and printed.<\/p>\n\n\n\n<p>Finally, the&nbsp;<code>OpenMultipleFiles<\/code>&nbsp;class instance is created and passed the file names (<code>test.txt<\/code>&nbsp;and&nbsp;<code>sample.txt<\/code>) and stored within the&nbsp;<code>obj<\/code>&nbsp;variable. The&nbsp;<code>read<\/code>&nbsp;method is then invoked on the&nbsp;<code>obj<\/code>&nbsp;to read the specified files.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Hi, i am a test file. Data added through function. Added data to the file.\nHi, i am a sample file for testing. Data added through function. Added data to the file.<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-comparison\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-comparison\"><\/a>Comparison<\/h2>\n\n\n\n<p>Let&#8217;s see how long it takes to process the contents of multiple files at the same time using the&nbsp;<code>open()<\/code>&nbsp;and the&nbsp;<code>fileinput.input()<\/code>&nbsp;function.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import timeit\n\n# open() Function Code\ncode = '''\nwith open('test.txt') as f1, open('sample.txt') as f2:\n    f1.read()\n    f2.read()\n'''\n\nprint(f\"Open Function Benchmark: {timeit.timeit(stmt=code, number=1000)}\")\n\n# fileinput Code\nsetup = 'import fileinput'\n\ncode = '''\nwith fileinput.input(files=('test.txt', 'sample.txt')) as file:\n    for data in file:\n        data\n'''\nprint(f\"Fileinput Benchmark: {timeit.timeit(setup=setup, stmt=code, number=1000)}\")<\/pre><\/div>\n\n\n\n<p>Using the&nbsp;<code>timeit<\/code>&nbsp;module, the above code measures the time it takes to process the contents of multiple files 1000 times for the&nbsp;<code>fileinput.input()<\/code>&nbsp;function and&nbsp;<code>open()<\/code>&nbsp;function. This method will aid in determining which is more efficient.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Open Function Benchmark: 0.3948998999840114\nFileinput Benchmark: 0.4962893000047188<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-limitations\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-limitations\"><\/a>Limitations<\/h2>\n\n\n\n<p>Every module is powerful in its own right, but it also has limitations, such as the&nbsp;<code>fileinput<\/code>&nbsp;module.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It does not read files, instead, it iterates through the contents of the file line by line and prints the results.<\/li>\n\n\n\n<li>Cannot write or append the data into the files.<\/li>\n\n\n\n<li>Cannot perform advanced file-handling operations.<\/li>\n\n\n\n<li>Less performant because the program&#8217;s performance may suffer when processing large files.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/fileinput-module#heading-conclusion\"><\/a>Conclusion<\/h2>\n\n\n\n<p>The&nbsp;<code>fileinput<\/code>&nbsp;module provides functions to process one or more than one file line by line to read the content. The&nbsp;<code>fileinput.input()<\/code>&nbsp;function is the primary interface of the&nbsp;<code>fileinput<\/code>&nbsp;module, and it provides parameters to give you more control over how the files are processed.<\/p>\n\n\n\n<p>Let&#8217;s recall what you&#8217;ve learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An overview of the&nbsp;<code>fileinput<\/code>&nbsp;module<\/li>\n\n\n\n<li>Basic usage of the&nbsp;<code>fileinput.input()<\/code>&nbsp;with and without context manager<\/li>\n\n\n\n<li>The&nbsp;<code>fileinput.input()<\/code>&nbsp;function and its parameters with examples<\/li>\n\n\n\n<li>A glimpse of&nbsp;<code>FileInput<\/code>&nbsp;class<\/li>\n\n\n\n<li>Comparison of&nbsp;<code>fileinput.input()<\/code>&nbsp;function with&nbsp;<code>open()<\/code>&nbsp;function for processing multiple files simultaneously<\/li>\n\n\n\n<li>Some limitations of the&nbsp;<code>fileinput<\/code>&nbsp;module<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/python-assert\" rel=\"noreferrer noopener\">How to use assert statements for debugging in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/init-vs-new\" rel=\"noreferrer noopener\">Difference between the __init__ and __new__ methods<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/context-managers-and-python-with-statement\" rel=\"noreferrer noopener\">What is context manager and the &#8216;with&#8217; statement in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python\" rel=\"noreferrer noopener\">How to implement getitem, setitem, and delitem in Python classes<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/unit-tests-in-python\" rel=\"noreferrer noopener\">How to perform unit testing using the unittest module in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/handling-files-in-python\" rel=\"noreferrer noopener\">File handling in Python &#8211; Opening, Reading, and much more<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/access-modifiers-in-python\" rel=\"noreferrer noopener\">Public, Protected, and Private access modifiers in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;fileinput&nbsp;module is a part of the standard library and is used when someone needs to iterate the contents of multiple files simultaneously. Well, Python&#8217;s in-built&nbsp;open()&nbsp;function can also be used for iterating the content but for only one file at a time. You&#8217;ll explore the classes and functions provided by the&nbsp;fileinput&nbsp;module to iterate over multiple files. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1173,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,42],"tags":[44,12,31],"class_list":["post-1171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-modules","tag-files","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1171"}],"version-history":[{"count":4,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1171\/revisions"}],"predecessor-version":[{"id":1282,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1171\/revisions\/1282"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1173"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}