-1

I'm creating a personal TV show and movie database and I use Python to get the information of the TV shows and movies. I have a file to get information for movies in a folder, which works fine.

I also have a Python file that gets the information of a TV show (which is a folder, e.g. Game of Thrones) and it also gets all the episode files from inside the folder and gets the information for those (it's formatted like this: e.g. Game of Thrones;3;9)

All this information is stored into 2 text files which MySQL can read: tvshows.txt and episodes.txt.

Python easily gets the information of the TV show in the first part of the program.

The second part of the program is to get each episode in the TV show folder and store the information in a file (episodes.txt):

def seTv(show):
  pat = '/home/ryan/python/tv/'
  pat = pat + show
  epList = os.listdir(pat)
  fileP = "/home/ryan/python/tvtext/episodes.txt"
  f = open(fileP, "w")
  print epList
  hdrs = ['Title', 'Plot', 'imdbRating', 'Season', 'Episode', 'seriesID']
  def searchTvSe(ep):
    ep = str(ep)
    print ep
    seq = ep.split(";")
    print seq
    tit = seq[0]
    seq[0] = seq[0].replace(" ", "+")
    url = "http://www.omdbapi.com/?t=%s&Season=%s&Episode=%s&plot=full&r=json" % (seq[0], seq[1], seq[2])
    respo = u.urlopen(url)
    respo = json.loads(str(respo.read()))
    if not os.path.exists("/var/www/html/images/"+tit):
      os.makedirs("/var/www/html/images/"+tit)
    imgNa = "/var/www/html/images/" + tit + "/" + respo["Title"] + ".jpg";
    for each in hdrs:
      #print respo[each] # ==== This checks to see if it is working, it is =====
      f.write(respo[each] + "\t")
      urllib.urlretrieve(respo["Poster"], imgNa)

  for co, tt in enumerate(epList):
    f.write("\N \t" + str(co) + "\t")
    searchTvSe(tt)
    f.write("\n")
  f.close()

fullTv()

The second part only works once and I have 3 folders inside the tv folder (Game of Thrones, Breaking Bad, The Walking Dead) and inside those files are one episode from the series (Game of Thrones;3;4, Breaking Bad;1;1, The Walking Dead;3;4).

This was working fine before I added 'seriesID' and changed the files (before I had a text file for each folder, which was needed as I had a table for each TV Show).

In episodes.txt, the information for Game of Thrones is the only one that appears. I deleted the Game of Thrones folder and it appears that the final one to be searched is the only one that has been added. It seems to be overwriting it?

Thanks.

rharper
  • 555
  • 1
  • 7
  • 15
  • 2
    You're opening the file with `'w'` (overwrite or create) argument. You should probably try `'a'` (append to the end) or ' – ForceBru Aug 29 '15 at 16:55

2 Answers2

2

Change this line:

f = open(fileP, "w")

To this:

f = open(fileP, "a")
janos
  • 120,954
  • 29
  • 226
  • 236
2

You need to open the file with 'a' instead of 'w':

with open('file.txt', 'a') as myfile:
     myfile.write("Hello world!")

You can find more details in the documentation at https://docs.python.org/2/library/functions.html#open.

Till Hoffmann
  • 9,479
  • 6
  • 46
  • 64