-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelete_lines_from_file.py
38 lines (30 loc) · 1.22 KB
/
Delete_lines_from_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def deleteLines(fileName, lineNumbers):
"""
Delete specific lines from a file and write the result to a new output file.
Parameters:
fileName (str): The name of the file to modify.
lineNumbers (int): The index of the line to delete (1-based).
Returns:
None
"""
try:
# Open the file in read mode and read all lines
with open(fileName, 'r') as filename:
lines = filename.readlines()
# Check if the line number is within range before attempting to delete
if lineNumbers > len(lines) or lineNumbers <= 0:
raise IndexError(f"Line number {lineNumbers} is out of range.")
# Delete the specified line (convert to 0-based index)
del lines[lineNumbers - 1]
# Write the modified content to a new output file
with open('output3.txt', 'w') as outfile:
outfile.writelines(lines)
# Handle invalid line numbers
except IndexError as e:
print(f"Error: {e}")
# Handle file not found errors
except FileNotFoundError as e:
print(f"Error: {e}. The file '{fileName}' was not found.")
# Example Usage
deleteLines('input.txt', 5)
# Enter the file name and the line number you want to delete.