{"id":59,"date":"2024-01-31T21:40:14","date_gmt":"2024-01-31T21:40:14","guid":{"rendered":"https:\/\/shaynepatelcybersecurityportfolio.online\/?p=59"},"modified":"2024-01-31T21:43:02","modified_gmt":"2024-01-31T21:43:02","slug":"updating-a-file-using-python","status":"publish","type":"post","link":"https:\/\/shaynepatelcybersecurityportfolio.online\/?p=59","title":{"rendered":"Updating a file using Python"},"content":{"rendered":"\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">This lab project from the Google Cybersecurity Course updates a file using a Python algorithm. This showcases some more technical knowledge and automation using Python. As you follow along, I\u2019ll provide details at every section of the lab and the overview to begin with. Like the last Linux project, these labs were written as if I was part of a fictional team and organization.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Project Description<\/mark><\/strong><\/h3>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">At my organization, access to restricted content is controlled with an allow list of IP addresses. The &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">allow_list.txt<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8221; file identifies these IP addresses. A separate remove list identifies IP addresses that should no longer have access to this content. I created an algorithm to automate updating the &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">allow_list.txt<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8221; file and remove these IP addresses that should no longer have access.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Open the file that contains the allow list<\/mark><\/strong><\/h3>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">For the first part of the algorithm, I opened the &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">allow_list.txt<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8221; file. First, I assigned this file name as a string to the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">import_file<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> variable:<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"270\" height=\"46\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-4.png\" alt=\"\" class=\"wp-image-60\" style=\"width:493px;height:auto\"\/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Then, I used a <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">with<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> statement to open the file:<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"406\" height=\"49\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-5.png\" alt=\"\" class=\"wp-image-61\" style=\"width:494px;height:auto\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-5.png 406w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-5-300x36.png 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">In my algorithm, the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">with<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> statement is used with the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.open()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function in read mode to open the allow list file for the purpose of reading it. The purpose of opening the file is to allow me to access the IP addresses stored in the allow list file. The <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">with<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> keyword will help manage the resources by closing the file after exiting the with statement. In the code <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">with open(import_file, &#8220;r&#8221;) as file:<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">, the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">open()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function has two parameters. The first identifies the file to import, and then the second indicates what I want to do with the file. In this case, &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">r<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8221; indicates that I want to read it. The code also uses the as keyword to assign a variable named <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">file; file<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> stores the output of the .<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">open()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function while I work within the with statement.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Read the file contents<\/mark><\/strong><\/h3>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">In order to read the file contents, I used the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.read()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method to convert it into the string.<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"468\" height=\"65\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-6.png\" alt=\"\" class=\"wp-image-62\" style=\"width:663px;height:auto\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-6.png 468w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-6-300x42.png 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">When using an <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.open()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function that includes the argument &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">r<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8221; for \u201cread,\u201d I can call the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.read()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function in the body of the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">with<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> statement. The <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.read()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method converts the file into a string and allows me to read it. I applied the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.read()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method to the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">file<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> variable identified in the with statement. Then, I assigned the string output of this method to the variable <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">.<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">In summary, this code reads the contents of the &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">allow_list.txt<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8221; file into a string format that allows me to later use the string to organize and extract data in my Python program.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Convert the string into a list<\/mark><\/strong><\/h3>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">In order to remove individual IP addresses from the allow list, I needed it to be in list format. Therefore, I next used the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.split()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method to convert the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> string into a list:<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"390\" height=\"42\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-7.png\" alt=\"\" class=\"wp-image-63\" style=\"width:534px;height:auto\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-7.png 390w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-7-300x32.png 300w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">The <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.split()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function is called by appending it to a string variable. It works by converting the contents of a string to a list. The purpose of splitting <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> into a list is to make it easier to remove IP addresses from the allow list. By default, the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.split()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function splits the text by whitespace into list elements. In this algorithm, the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.split()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function takes the data stored in the variable <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">, which is a string of IP addresses that are each separated by a whitespace, and it converts this string into a list of IP addresses. To store this list, I reassigned it back to the variable <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Iterate through the remove list<\/mark><\/h3>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">A key part of my algorithm involves iterating through the IP addresses that are elements in the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">remove_list<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">. To do this, I incorporated a <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">for<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> loop:<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"241\" height=\"84\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-8.png\" alt=\"\" class=\"wp-image-64\" style=\"width:451px;height:auto\"\/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">The <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">for<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> loop in Python repeats code for a specified sequence. The overall purpose of the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">for<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> loop in a Python algorithm like this is to apply specific code statements to all elements in a sequence. The <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">for<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> keyword starts the for loop. It is followed by the loop variable <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">element<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> and the keyword in. The keyword in indicates to iterate through the sequence <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> and assign each value to the loop variable <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">element<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Remove IP addresses that are on the remove list<\/mark><\/h3>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">My algorithm requires removing any IP address from the allow list, <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">, that is also contained in <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">remove_list<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">.\u00a0 Because there were not any duplicates in <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">, I was able to use the following code to do this:<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"397\" height=\"124\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-9.png\" alt=\"\" class=\"wp-image-65\" style=\"width:692px;height:auto\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-9.png 397w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-9-300x94.png 300w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">First, within my <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">for<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> loop, I created a conditional that evaluated whether or not the loop variable <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">element<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> was found in the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> list. I did this because applying <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.remove()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> to elements that were not found in <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> would result in an error.<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Then, within that conditional, I applied <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.remove()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> to <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">. I passed in the loop variable <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">element<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> as the argument so that each IP address that was in the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">remove_list<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> would be removed from <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Update the file with the revised list of IP addresses<\/h3>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">As a final step in my algorithm, I needed to update the allow list file with the revised list of IP addresses. To do so, I first needed to convert the list back into a string. I used the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.join()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method for this:<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"468\" height=\"45\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-10.png\" alt=\"\" class=\"wp-image-66\" style=\"width:697px;height:auto\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-10.png 468w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-10-300x29.png 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">The <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.join()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method combines all items in an iterable into a string. The <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.join()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method is applied to a string containing characters that will separate the elements in the iterable once joined into a string. In this algorithm, I used the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.join()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method to create a string from the list <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> so that I could pass it in as an argument to the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.write()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method when writing to the file &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">allow_list.txt<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8220;. I used the string (<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">&#8220;\\n&#8221;<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">) as the separator to instruct Python to place each element on a new line.<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">Then, I used another <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">with<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> statement and the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.write()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method to update the file:<\/mark><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"319\" height=\"86\" src=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-11.png\" alt=\"\" class=\"wp-image-67\" style=\"width:608px;height:auto\" srcset=\"https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-11.png 319w, https:\/\/shaynepatelcybersecurityportfolio.online\/wp-content\/uploads\/2024\/01\/image-11-300x81.png 300w\" sizes=\"auto, (max-width: 319px) 100vw, 319px\" \/><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">This time, I used a second argument of &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">w<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8221; with the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">open()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function in my <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">with<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> statement. This argument indicates that I want to open a file to write over its contents. When using this argument &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">w<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8220;, I can call the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.write()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function in the body of the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">with<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> statement. The <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.write()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function writes string data to a specified file and replaces any existing file content.<\/mark><\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">In this case I wanted to write the updated allow list as a string to the file &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">allow_list.txt<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8220;. This way, the restricted content will no longer be accessible to any IP addresses that were removed from the allow list. To rewrite the file, I appended the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.write()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> function to the file object <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">file<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> that I identified in the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">with<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> statement. I passed in the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> variable as the argument to specify that the contents of the file specified in the with statement should be replaced with the data in this variable.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">I created an algorithm that removes IP addresses identified in a <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">remove_list<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> variable from the &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">allow_list.txt<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8221; file of approved IP addresses. This algorithm involved opening the file, converting it to a string to be read, and then converting this string to a list stored in the variable <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">. I then iterated through the IP addresses in <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">remove_list<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">. With each iteration, I evaluated if the element was part of the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> list. If it was, I applied the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.remove()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method to it to remove the element from <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">. After this, I used the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">.join()<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> method to convert the <\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">ip_addresses<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\"> back into a string so that I could write over the contents of the &#8220;<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color has-black-color\">allow_list.txt<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8221; file with the revised list of IP addresses.<\/mark><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This lab project from the Google Cybersecurity Course updates a file using a Python algorithm. This showcases some more technical knowledge and automation using Python. As you follow along, I\u2019ll provide details at every section of the lab and the overview to begin with. Like the last Linux project, these labs were written as if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":2,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shaynepatelcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}