🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The fileinput module in Python provides a simple way to iterate over lines from multiple input streams, including standard input, and optionally make in-place modifications to files. It is particularly useful for writing scripts that process files line-by-line.
Table of Contents
- Introduction
- Key Functions and Methods
inputfilenamelinenofilelinenoisfirstlineisstdinnextfileclose
- Examples
- Basic Usage
- Processing Multiple Files
- In-Place File Editing
- Real-World Use Case
- Conclusion
- References
Introduction
The fileinput module allows you to loop over the lines of multiple input streams easily. It can handle standard input, files specified on the command line, or a combination of both. The module provides utility functions and methods to control and inspect the input streams.
Key Functions and Methods
input
The input function is the primary entry point for the module. It returns an iterator that yields lines from the input streams.
import fileinput
for line in fileinput.input():
print(line, end='')
filename
Returns the name of the file currently being read.
import fileinput
for line in fileinput.input(files=('file1.txt', 'file2.txt')):
print(f"Filename: {fileinput.filename()}, Line: {line}", end='')
lineno
Returns the cumulative line number of the line that has just been read.
import fileinput
for line in fileinput.input():
print(f"Line {fileinput.lineno()}: {line}", end='')
filelineno
Returns the line number in the current file.
import fileinput
for line in fileinput.input():
print(f"File Line {fileinput.filelineno()}: {line}", end='')
isfirstline
Returns True if the line just read is the first line of its file, otherwise False.
import fileinput
for line in fileinput.input():
if fileinput.isfirstline():
print(f"First line of {fileinput.filename()}")
print(line, end='')
isstdin
Returns True if the line was read from standard input, otherwise False.
import fileinput
for line in fileinput.input():
if fileinput.isstdin():
print("Reading from standard input")
print(line, end='')
nextfile
Skips the rest of the current file, closing it and moving on to the next file.
import fileinput
for line in fileinput.input():
if 'skip' in line:
fileinput.nextfile()
print(line, end='')
close
Closes the sequence of input streams, preventing further iteration.
import fileinput
for line in fileinput.input():
if 'stop' in line:
fileinput.close()
print(line, end='')
Examples
Basic Usage
import fileinput
for line in fileinput.input(files=('file1.txt', 'file2.txt')):
print(line, end='')
Processing Multiple Files
import fileinput
for line in fileinput.input(files=('file1.txt', 'file2.txt')):
if fileinput.isfirstline():
print(f"\nProcessing file: {fileinput.filename()}")
print(f"Line {fileinput.filelineno()}: {line}", end='')
In-Place File Editing
import fileinput
for line in fileinput.input(files=('file1.txt',), inplace=True):
print(line.replace('old_text', 'new_text'), end='')
Real-World Use Case
Log File Analysis
import fileinput
error_count = 0
for line in fileinput.input(files=('log1.txt', 'log2.txt')):
if 'ERROR' in line:
error_count += 1
print(f"Error in {fileinput.filename()}, Line {fileinput.filelineno()}: {line}", end='')
print(f"\nTotal Errors: {error_count}")
Conclusion
The fileinput module in Python provides a powerful yet simple interface for processing lines from multiple input streams, including standard input. It supports features like cumulative line numbering, file-specific line numbering, and in-place file editing, making it used for writing scripts that handle file input.
References
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment