-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (63 loc) · 2.24 KB
/
main.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def read_files(file_path: str) -> str:
"""
Read the contents of a text file.
Args:
file_path (str): The path to the text file.
Returns:
str or None: The contents of the text file if it exists, None otherwise.
"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
return text
except FileNotFoundError:
print("File not found.")
return None
def write_to_file(lines, output_file):
"""
Write lines to a text file.
Args:
lines (list of str): The list of lines to be written to the output file.
output_file (str): The name of the output text file.
Returns:
None
Raises:
IOError: If an error occurs while writing to the output file.
"""
try:
with open(output_file, 'w') as f_out:
f_out.writelines(lines)
except IOError:
print(f"Error writing to file '{output_file}'.")
def filter_lines(input_file, keyword):
"""
Filter lines containing a specified keyword from a text file and write them to a new file.
Args:
input_file (str): The name of the input text file (with .txt extension).
keyword (str): The keyword used for filtering lines.
output_file (str): The name of the output text file where filtered lines will be written.
Returns:
None
Raises:
FileNotFoundError: If the specified input file is not found.
"""
try:
with open(input_file, 'r') as f_in:
lines = f_in.readlines()
except FileNotFoundError:
print(f"Error: File '{input_file}' not found.")
return
filtered_lines = [line for line in lines if keyword in line]
return filtered_lines
def main():
input_file = input("Enter the input file name (with .txt extension): ")
keyword = input("Enter the keyword to filter lines: ")
output_file = "filtered.txt"
filtered_lines = filter_lines(input_file, keyword)
if filtered_lines:
write_to_file(filtered_lines, output_file)
print(f"Filtered lines containing '{keyword}' have been written to '{output_file}'.")
else:
print(f"No lines containing '{keyword}' found in the file.")
if __name__ == "__main__":
main()