-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
47 lines (26 loc) · 1.43 KB
/
Copy pathmain.py
File metadata and controls
47 lines (26 loc) · 1.43 KB
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
# Define a function named `update_file` that takes in two parameters: `import_file` and `remove_list`
# and combines the steps you've written in this lab leading up to this
def update_file(import_file, remove_list):
# Build `with` statement to read in the initial contents of the file
with open(import_file, "r") as file:
# Use `.read()` to read the imported file and store it in a variable named `ip_addresses`
ip_addresses = file.read()
# Use `.split()` to convert `ip_addresses` from a string to a list
ip_addresses = ip_addresses.split()
# Build iterative statement
# Name loop variable `element`
# Loop through `ip_addresses`
for element in ip_addresses:
# Build conditional statement
# If current element is in `remove_list`,
if element in remove_list:
# then current element should be removed from `ip_addresses`
ip_addresses.remove(element)
# Convert `ip_addresses` back to a string so that it can be written into the text file
ip_addresses = " ".join(ip_addresses)
# Build `with` statement to rewrite the original file
with open(import_file, "w") as file:
# Rewrite the file, replacing its contents with `ip_addresses`
file.write(ip_addresses)
# Call `update_file()` and pass in ".txt file" and a list of IP addresses to be removed
update_file(".txt", ["192.168.25.60", "192.168.140.81", "192.168.203.198"])