-
Notifications
You must be signed in to change notification settings - Fork 16
/
lab04_b_d.py
executable file
·40 lines (33 loc) · 1.22 KB
/
lab04_b_d.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
'''
Program pengekstrak informasi rahasia Agen Benny.
Program menerima input berupa nama file teks input dan output.
File input berisi teks yang mengandung tag <start> dan <end> untuk
menandakan informasi rahasia.
'''
user_input = input("Masukkan nama file input dan output: ")
input_name = user_input.split(" ", 1)[0]
output_name = user_input.split(" ", 1)[1]
try:
input_file = open(input_name, "r")
output_file = open(output_name, "w+")
except FileNotFoundError:
print("File {} bermasalah! Benny lolos kali ini.".format(input_name))
else:
message = []
for input_line in input_file:
input_line = input_line.strip()
pos = -1
while True:
pos = input_line.find("<start>", pos + 1)
if pos == -1:
break
part = input_line[input_line.find("<start>", pos) +
len("<start>"):input_line.find("<end>", pos)]
part = part.strip()
message.append(part)
output = " ".join(message)
print(output, end="", file=output_file)
input_file.close()
print("Rahasia telah terbongkar, silakan cek file {}".format(output_name))
finally:
output_file.close()