HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Sending users to web pages to download Pokemon episodes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sendingpagesepisodespokemonwebdownloadusers

Problem

I have a python code named seasons.py which uses inputs to open different web pages in which there are links to download Pokémon 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 Pokémon episodes names are formatted like:

P0k3M0N.1x01.Es.avi.mp4


How 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 webbrowser
import os
import seasons
import time

f1 = open('last.txt', 'r')
f4 = open('last2.txt', 'r')

out = False
list1 = []
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 = 1
list3 = []
list4 = []

for i in range(int(list2[-1])):
y = str(x)
if y in list2:
list3.append(y)
x += 1

count1 = 0
count2 = 0

for i in list4:
count1 += 1

for i in list3:
count2 += 1

if 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)

Solution

f1 = open('last.txt', 'r')
f4 = open('last2.txt', 'r')

out = False
list1 = []
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()


I would avoid dealing with files in this way. If there is an error anywhere in between the first two and the last two lines shown above, your code will stop and the file will be left open.

It's better practice to use the with statement - that way, if there are any errors, the file will still be closed.

with open('last.txt', 'r') as f:
    print("Last one: " + f.readline())

with open('last2.txt', 'r') as f:
    print("Last: " + f.readline())

Code Snippets

f1 = open('last.txt', 'r')
f4 = open('last2.txt', 'r')

out = False
list1 = []
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()
with open('last.txt', 'r') as f:
    print("Last one: " + f.readline())

with open('last2.txt', 'r') as f:
    print("Last: " + f.readline())

Context

StackExchange Code Review Q#138555, answer score: 5

Revisions (0)

No revisions yet.