Open and Read file variables

Screen Link:

App Profile Recommendation | Dataquest
My Code:

# open apple app data and list it
opened_file=open('AppleStore.csv')
from csv import reader
read_file= reader(opened_file)
ios=list(read_file)
header= ios[0]
iosdata=ios[1:]
# open android app data and list it
opened_file= open('googleplaystore.csv')
from csv import reader
read_file= reader(opened_file)
android= list(read_file)
header=android[0]
android_data=android[1:]

What I expected to happen:

What actually happened:
How this code takes same variable name opened_file and read_file to access two different files. Isn’t it a rule that the most recently defined variable would take the value and be used in the program. So, according to me read_file = read(opened_file) will read the data of googleplaystore.csv and supersedes applestore.csv for the rest of the program. Please clarify.

Thanks for reading.

Replace this line with the output/error

Yes, you are correct! When you reuse a variable name, the previous data is “overwritten.” Well…technically speaking, the data is not overwritten but rather the variable is just pointing to a different location in memory that represents different data.

However, we are ultimately using these two variables (i.e. open_file and read_file) as “middle men” before we use unique names for the variable that will point to two different list objects; one for ios and one for android.

Think of it like making two different meals. You start by frying some food in a pan, then transfer it to a pot to boil it, and then put the result in a bowl to eat it. You can then use the same pan to fry some different food, then transfer that food to the same pot to boil it, and then put it in a different bowl to eat it. Even though you used the same pot & pan, in the end, you have two different meals in two different bowls.

You can think of this like “recycling your variables.”

Also note that you only need to import reader once and don’t need to import it for each file you are opening.

All that said, you do have a problem with using the name header twice; once for ios and once for android. Please consider using different names for the headers (e.g. ios_header and android_header)