Files in Python:
One of the most important subjects for programmers and automation testers is Python file handling (also known as File I/O). It is necessary to work with files in order to write to or read data from them.
Also, if you didn’t know, I/O operations are the most expensive processes where a program can go wrong. As a result, you must use extreme caution while implementing file processing for reporting or any other reason. Optimizing a single file action can help in the creation of a high-performing application or a reliable automated software testing solution.
Consider the following scenario: you’re planning to construct a large Python project with a large number of workflows. Then it’s unavoidable that you don’t make a log file. You’ll also be handling the log file’s read and write activities. Debugging huge applications with log files is a terrific way to go. It’s usually better to consider a scalable design from the start, as you won’t be sorry later if you didn’t.
Given a file the task ist or remove lines starting with any given prefix and store them in new file in Python
- Python Program to Count Number of Digits in a Text File
- How to run Python scripts
- Python Interview Questions on File Manipulation
Program to Remove Lines Starting with any Prefix in Python
Method #1: Using Built-in Functions
Approach:
- Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
- Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
- Make another variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system.
- Open another file in write mode. In this case, we’re writing the contents into the file.
- Give some random prefix as user input using the input() function and store it in a variable.
- Iterate through the lines of the file using the for loop and readlines() function.
- Check if the corresponding line starts with the given prefix using startswith() function and if conditional statement
- If it is true then print the line.
- Write the corresponding line to the writeFile using the write() function.
- Close the above writing file using the close() function.
- Close the above read file using the close() function.
- The Exit of Program.
Below is the implementation:
# Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Open the file in read-only mode. In this case, we're simply reading the contents of the file. readFile = open(givenFilename, 'r') # Make another variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system. writeFileName = "samplewritefile.txt" # Open another file in write mode. In this case, we're writing the contents into the file. writingFile = open(writeFileName,'w') # Give some random prefix as user input using the input() function and store it in a variable gvn_prefix = input("Enter some random prefix = ") # Iterate through the lines of the file using the for loop and readlines() function for lines in readFile .readlines(): # Check if the corresponding line starts with the given prefix using startswith() function # and if conditional statement if not (lines.startswith(gvn_prefix)): # If it is true then print the line print(lines) # Write the corresponding line to the writeFile using the write() function writingFile.write(lines) # Close the above writing file using the close() function writingFile.close() # Close the above read file using the close() function readFile.close()
Output:
Enter some random prefix = hello specific python codes Summary good morning this is btechgeeks welcome to btechgeeks
Method #2: Using Built-in Functions
Approach:
- Import re module using the import Keyword.
- Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
- Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
- Make another variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system.
- Open another file in write mode. In this case, we’re simply writing the contents of the file.
- Give some random prefix as user input using the input() function and store it in a variable.
- Iterate through the lines of the file using the for loop and readlines() function.
- search all the lines which are having the given prefix using the findall() function and re module(here we used ^ symbol to denote the first word).
- Get all the lines which are not having a given prefix using the if and not keywords (here we removed lines that are not given prefix).
- If it is true then print the line.
- Write the corresponding line to the writeFile using the write() function.
- Close the above writing file using the close() function.
- Close the above read file using the close() function.
- The Exit of Program.
Below is the implementation:
# Import re module using the import Keyword import re # Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Open the file in read-only mode. In this case, we're simply reading the contents of the file. readFile = open(givenFilename, 'r') # Make another variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system. writeFileName = "samplewritefile.txt" # Open another file in write mode. In this case, we're simply writing the contents of the file. writingFile = open(writeFileName, 'w') # Give some random prefix as user input using the input() function and store it in a variable gvn_prefix = input("Enter some random prefix = ") # Iterate through the lines of the file using the for loop and readlines() function for lines in readFile.readlines(): # search all the lines which are having the given prefix using the findall() function # and re module(here we used ^ symbol to denote the first word) search = re.findall("^"+gvn_prefix, lines) # Get all the lines which are not having given prefix using the if and not keywords # (here we removed lines which are not given prefix) if not search: # If it is true then print the line print(lines) # Write the corresponding line to the writeFile using the write() function writingFile.write(lines) # Close the above writing file using the close() function writingFile.close() # Close the above read file using the close() function readFile.close()
Output:
Enter some random prefix = hello specific python codes Summary good morning this is btechgeeks welcome to btechgeeks
File Content: (samplefile.txt)
hello this is btechgeeks sample file specific python codes Summary hello good morning good morning this is btechgeeks welcome to btechgeeks
File Content: (samplewritefile.txt)
specific python codes Summary good morning this is btechgeeks welcome to btechgeeks
Google Colab Images:
Files and Code: