I have a python code named seasons.py which uses inputs to open different web pages in which there are links to download Pokmon episodes.The program has a module called seasons.py where it returns the number of episodes that has the season which was entered. It also needs a number of text files called last.txt, and seasons.txt, last2.txt.The Pokmon episodes names are formatted like:P0k3M0N.1x01.Es.avi.mp4How can I improve the code and prevent errors? If you could tell me better names for the variables it will be perfect, but if not, it's OK.import webbrowserimport osimport seasonsimport timef1 = open('last.txt', 'r')f4 = open('last2.txt', 'r')out = Falselist1 = []list2 = []for file in os.listdir(.):    if P0k3M0N in file:        list1.append(file)for i in list1:    index = i.find(x)    str1 = i[index + 1:index + 3]    list2.append(str1)print(Last one:  + f1.readline())print(Last:  + f4.readline())f1.close()f4.close()x = 1list3 = []list4 = []for i in range(int(list2[-1])):    y = str(x)    if y in list2:        list3.append(y)    x += 1count1 = 0count2 = 0for i in list4:    count1 += 1for i in list3:    count2 += 1if count1 != 0:    print(You have:  + str(list4))if count2 != 0:    print(You have consecutives from  + list3[0] +  to  + list3[-1])while out == False:    print([1] Full season)    print([2] Single episodes: Examples: 1-10 / 1, 2 / 3)    print([3] Get out)    input2 = input()    input1 = str(input2)    if input1 == 3:        out = True    else:        if input1 == 1:            season = input(Season: )            episodes = seasons.episodes(season)            for i in range(episodes):                if len(str(i + 1)) == 1:                    url = http://seriesblanco.com/serie/979/temporada- + str(season) + /capitulo-0 + str(x) + /pokemon.html                    webbrowser.open_new_tab(url)                    time.sleep(20)                else:                    url = http://seriesblanco.com/serie/979/temporada- + str(season) + /capitulo- + str(x) + /pokemon.html                    webbrowser.open_new_tab(url)                    time.sleep(20)        else:            input4 = input(Season: )            season = str(input4)            file = open('season.txt', 'r')            total_eps = file.readlines()            number = total_eps[int(input4) - 1]            print(This season has  + number +  episodes)            input3 = input(Episodes: )            episodes = str(input3)            if - in episodes:                index = episodes.find(-)                first = episodes[:index]                second = episodes[index + 1:]                x = int(first)                numbers = 123456789                for i in range(int(second) - int(first) + 1):                    if str(x) in numbers:                        url = http://seriesblanco.com/serie/979/temporada- + season + /capitulo-0 + str(x) + /pokemon.html                        webbrowser.open_new_tab(url)                    else:                        url = http://seriesblanco.com/serie/979/temporada- + season + /capitulo- + str(x) + /pokemon.html                        webbrowser.open_new_tab(url)                    x += 1                    if int(second) > 10:                        time.sleep(10)                os.remove(last2.txt)                f3 = open('last2.txt', 'w')                f3.write(episodes)                f3.close()            elif , in episodes:                numbers = 123456789                count = 0                list = []                index = episodes.find(,)                number = episodes[:index]                list.append(number)                while index != -1:                    index = episodes.find(,)                    if episodes[index + 1] ==  :                        episodes = episodes[index + 2:]                    elif episodes[index + 1] in numbers:                        episodes = episodes[index + 1:]                    number = episodes[:index]                    list.append(number)                list.pop(-1)                for i in list:                    count += 1                for i in range(count):                    if len(list[i]) == 1:                        url = http://seriesblanco.com/serie/979/temporada- + season + /capitulo-0 + list[i] + /pokemon.html                        webbrowser.open_new_tab(url)                    else:                        url = http://seriesblanco.com/serie/979/temporada- + season + /capitulo- + list[i] + /pokemon.html                        webbrowser.open_new_tab(url)            else:                if len(episodes) == 1:                    url = http://seriesblanco.com/serie/979/temporada- + season + /capitulo-0 + episodes + /pokemon.html                    webbrowser.open_new_tab(url)                    os.remove(last.txt)                    f2 = open('last.txt', 'w')                    f2.write(episodes)                    f2.close()                else:                    url = http://seriesblanco.com/serie/979/temporada- + season + /capitulo- + episodes + /pokemon.html                    webbrowser.open_new_tab(url)                    os.remove(last.txt)                    f2 = open('last.txt', 'w')                    f2.write(episodes)                    f2.close()The other module is here:def episodes(n_season):    f = open('season.txt', 'r')    seasons = f.readlines()    season = seasons[int(n_season) - 1]    index = season.find('\n')    return season[:index]seasons.txt contains this:83 34 40 51 64 39 51 53 46 51 51 52 33 49 48 44 47 44
Sending users to web pages to download Pokemon episodes
python;python 3.x
You can save a lot of code duplication by using format:if len(episodes) == 1:    url = http://seriesblanco.com/serie/979/temporada- + season + /capitulo-0 + episodes + /pokemon.html    webbrowser.open_new_tab(url)    os.remove(last.txt)    f2 = open('last.txt', 'w')    f2.write(episodes)    f2.close()else:    url = http://seriesblanco.com/serie/979/temporada- + season + /capitulo- + episodes + /pokemon.html    webbrowser.open_new_tab(url)    os.remove(last.txt)    f2 = open('last.txt', 'w')    f2.write(episodes)    f2.close()Becomes (with the with..as construct as well):url = http://seriesblanco.com/serie/979/temporada-{}/capitulo-{:02d}/pokemon.htmlwebbrowser.open_new_tab(url.format(season, episode))with open('last.txt', 'w') as f2:    f2.write(episodes)Here, the {:02d} makes sure that the number has two digits and will fill with 0 if it has not. This also takes away all the code with numbers=... to check whether the number is one or two digits. The format syntax also happily takes in any type as arguments and calls str() on them, so this works:>>> {:02d}.format(1)'01'>>> {:02d}.format(10)'10'Also note that when specifying the 'w' flag, the file will be overwritten, so there is no need to delete it first.The building of the url and the opening in a webbrowser is ripe to be outsourced to a function:def open_in_new_tab(webbrowser, season, episode):    url = http://seriesblanco.com/serie/979/temporada-{}/capitulo-{:02d}/pokemon.html    webbrowser.open_new_tab(url.format(season, episode))