Skip to content

JustinRoberg/Update-a-file-through-a-Python-algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Update a file through a Python algorithm Lab

Project description

This was a lab for the Google Cybersecurity Certificate. The prompt was to create an Algorithm using what we had learned in Python to read and update an allow list.

We were given an allow list of IP addresses which has access to restricted content, “allow_list.txt”. We were then given a remove list which identifies IP addresses that should no longer have access to the restricted content. I automated updating of the “allow_list.txt” to remove IP addresses that should no longer have access using a Python algorithm.

Environment/Code:

  • Jupyter notebook

  • Python

Open the file that contains the allow list

The first part of the project I was tasked with opening the allow list file. I assigned "allow_list.txt" as a string to the variable import_file:

Open file

To open the file I used a with statement:

Open file

The purpose of this code is to open the allow list file, access the IP addresses stored in the file, and then save the output as the variable file outside the statement. with is used to manage resources. open() is the function to open files
(import_file, “r”) are the parameters for my open() function. import_file, the name of the file to be opened, and ”r”the parameter for read. as file - as keyword assigns file variable to reference the open() function output.

Read the file contents

I use .read() to convert file into the string.

Read

Purpose of this code is to convert “allow_list.txt” into a string format that I can later use to organize and extract data. I assigned the string to the variable ip_addresses .

.read() - can convert a file into a string. .read() function can be called in the body of a statement when the .open() function is used with read argument ”r”.

Convert the string into a list

Used .split() to make a list from the ip_addresses string.

Convert

Purpose of this code was to create a list of IP addresses that I could remove addresses from. I converted the string into a list then assigned it back to the ip_addresses variable .split() - converts a string to a list.

Iterate through the remove list

A for loop was used to check the IP addresses for those that are in the remove_list.

Iterate through remove list

for - Create a for loop which is needed to iterate through a specified sequence. element - the loop variable in remove_list - iterate through and assign values to the loop variable.

Remove IP addresses that are on the remove list

My algorithm requires removing any IP address from the allow list, ip_addresses, that is also contained in remove_list.

Remove IP addresses

Within the loop body I created a conditional to see if the element variable was found in the list of ip addresses. I then used .remove() with the loop variable element passed through it on ip_addresses so that every IP on the remove_list would be removed.

Update the file with the revised list of IP addresses

I used .join() to convert the list back to a string so that I could update the allow list.

Update file

I used ”\n” to give each IP address a new line.

I then used a withstatement followed by a .write() to update the allow list.

Update file

Full code:

with open(import_file, "r") as file:

    ip_addresses = file.read()

ip_addresses = ip_addresses.split()


for element in remove_list:
    
    if element in ip_addresses:

        ip_addresses.remove(element)


ip_addresses = "\n".join(ip_addresses)


with open(ip_addresses, "w") as file:
    
  file.write(ip_addresses)

About

Update a file through a Python algorithm Lab

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published