Skip to content

DeJuvn/Update-a-File-Through-a-Python-Algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

Update-a-File-Through-a-Python-Algorithm

Project description

To control access to restricted content at my organization, we maintain an allow list of IP addresses in a file named "allow_list.txt". Additionally, we maintain a remove list that identifies IP addresses that should no longer have access to the content. To automate the process of updating the "allow_list.txt" file and removing these IP addresses, I created an algorithm.

Open the file that contains the allow list

To begin the algorithm, I stored the name of the "allow_list.txt" file in a string variable called "import_file".

image

Then, I used a with statement to open the file.

image

To access the IP addresses stored in the allow list file, my algorithm utilizes the with statement in conjunction with the open() function in read mode. The purpose of opening the file is to read it, and the with keyword is used to manage the resources by automatically closing the file after exiting the with statement. The open() function takes two parameters: the first identifies the file to import, and the second specifies the intended operation on the file. In this case, "r" indicates that the file should be read. The as keyword is also used to assign the output of the open() function to a variable named file, which is used to manipulate the contents of the file within the with statement.

Read the file contents

I used the .read() method to convert it into the string.

image

Convert the string into a list

To enable the removal of individual IP addresses from the allow list, I converted the ip_addresses string into a list using the .split() method.

image

Iterate through the remove list

I included a for loop in my algorithm to iterate through the IP addresses in the remove_list, which is a list of IP addresses that should no longer have access to the restricted content.

image

Remove IP addresses that are on the remove list

To ensure that the IP addresses listed in the remove_list are removed from the ip_addresses list, I used the .remove() method within a for loop. Since all the elements of remove_list are also present in ip_addresses, and there are no duplicates in ip_addresses, I could use .remove() to eliminate any matching IP address. Here's how I implemented it:

image

Update the file with the revised list of IP addresses

To update the allow list file with the revised list of IP addresses, I first converted the list of IP addresses back into a string using the .join() method. This method takes the elements of a list and concatenates them into a string with a specified delimiter. In this case, I used the newline character (\n) as the delimiter, which creates a new line for each IP address in the file.

image

I used another with statement and the .write() method to update the file:

image

Summary

I developed an algorithm to update the "allow_list.txt" file by removing the IP addresses present in the remove_list. The algorithm involved opening the file, reading its contents as a string, and then converting it into a list of IP addresses stored in the variable ip_addresses. I then used a for loop to iterate through each IP address in the remove_list and removed it from the ip_addresses list using the .remove() method. Finally, I used the .join() method to convert the ip_addresses list back into a string, allowing me to overwrite the contents of the "allow_list.txt" file with the updated list of IP addresses.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published