Files in Python:
Files are identified locations on the disc where associated data is stored. They’re used to keep data in non-volatile memory for a long time (e.g. hard disk).
We use files for future usage of the data by permanently saving it because Random Access Memory (RAM) is volatile (it loses its contents when the machine is turned off).
We must first open a file before we can read from or write to it. When we’re finished, it needs to be closed so that the file’s resources may be released.
As a result, a file operation in Python is performed in the following order:
- Create a new file
- You can either read or write (perform the operations)
- Close the file.
Given a file, the task is to delete a specific file from the given file in python
- Python Program to Read First n Lines of a File
- Python Program to Reverse the Content of Text File and Store it in Another File
- Python Program to Capitalize the First Letter of Every Word in the File
Program to Delete Specific Line from a Text File in Python
Below is the full approach for deleting a specific file from the given file in python
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 just reading the contents of the file.
- Get all the lines from the given file using the readlines() function.
- Take a variable (which gives the line number) and initialize its value with 1.
- Give the line number which you want to delete as static input and store it in a variable.
- Open the file in write mode. In this case, we’re simply writing the contents into the file.
- Iterate in the above file lines using the for loop.
- Check if the index(line number) is not given line number using the if conditional statement
- If it is true, then write that line into the write() function.
- Increment the above index (line number) value by 1
- Print some random text for acknowledgment.
- 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 just reading the contents of the file. with open(givenFilename, 'r') as givenfilecontent: # Get all the lines from the given file using the readlines() function filelines = givenfilecontent.readlines() # Take a variable (which gives the line number)and initialize its value with 1 indx = 1 #Give the line number which you want to delete as static input and store it in a variable. linenumb=3 # Open the file in write mode. In this case, we're simply writing the contents into the file. with open(givenFilename, 'w') as givenfilecontent: # Iterate in the above file lines using the for loop for line in filelines: # Check if the index(line number) is not given line number using the if conditional statement if indx != linenumb: # If it is true, then write that line into the write() function givenfilecontent.write(line) # Increment the above index(line number) value by 1 indx += 1 # Print some random text for acknowledgment print("The ",linenumb,'line is deleted')
Output:
The 3 line is deleted
File Content:
welcome to btechgeeks give code in Python write a code in Python By now you might be aware that Python is a Popular Programming Language used right from web developers to data scientists.
File content after deleting the 3rd line:
welcome to btechgeeks give code in Python By now you might be aware that Python is a Popular Programming Language used right from web developers to data scientists.
Google Colab Images:
Files and Code: