I'm very new to Python and not a programmer. I have this:
y1990=open('Documents/python/google-python-exercises/babynames/baby1990.html', 'r', encoding='utf8')
y1992=open('Documents/python/google-python-exercises/babynames/baby1992.html', 'r', encoding='utf8')
y1994=open('Documents/python/google-python-exercises/babynames/baby1994.html', 'r', encoding='utf8')
y1996=open('Documents/python/google-python-exercises/babynames/baby1996.html', 'r', encoding='utf8')
y1998=open('Documents/python/google-python-exercises/babynames/baby1998.html', 'r', encoding='utf8')
y2000=open('Documents/python/google-python-exercises/babynames/baby2000.html', 'r', encoding='utf8')
y2002=open('Documents/python/google-python-exercises/babynames/baby2002.html', 'r', encoding='utf8')
y2004=open('Documents/python/google-python-exercises/babynames/baby2004.html', 'r', encoding='utf8')
y2006=open('Documents/python/google-python-exercises/babynames/baby2006.html', 'r', encoding='utf8')
y2008=open('Documents/python/google-python-exercises/babynames/baby2008.html', 'r', encoding='utf8')
I want to write a more succint code, so I've thought of this:
path='Documents/python/google-python-exercises/babynames/baby'
years=[year for year in range(1990,2010,2)]
open(path+str(years[0])+'.html') # works
On the other hand
'y'+str(years[0]) #works fine and creates string 'y1990'
However when I try to
'y'+str(years[0])=open(path+str(years[0])+'.html')
File "<stdin>", line 1
SyntaxError: can't assign to operator
As you can see I'm trying to create the variable name and open files dynamically. I've tried multiple ways along these lines and all produce similar errors. I've also found other posts dealing with what I think are similar issues but I fail to see how the answers solve my situation (might very well be my lack of experience with Python). People mention that lists or dictionaries are the way to go, does this apply to my problem too? How would I go about to solve this? Is this even the right Python way?