riverscuomo / spotkin_server

A python package that updates one or more of your Spotify playlists every day with a random selection of songs from any playlists you specify. Here's mine: https://open.spotify.com/playlist/1HaQfSGjNzIsiC5qOsCUcW?si=ddc16d3e9524410c . This repo also contains the backend server for Spotkin webapp.
GNU General Public License v3.0
82 stars 9 forks source link

If running more than one job, data is empty after the first job is run #1

Closed riverscuomo closed 2 years ago

riverscuomo commented 2 years ago

in main()

joshiemoore commented 2 years ago

This happens because open()ed files are iterables which can only be iterated once, and then they are exhausted.

To fix this, you could re-open the file for each job like this:

for job in jobs:
    with open(ADD_LIST_FILE) as csvfile:
        data = csv.DictReader(csvfile)
        # work . . .

Or you could read the file into a list which you can iterate as many times as you want:

with open(ADD_LIST_FILE) as csvfile:
    reader = csv.DictReader(csvfile)
    data = list(reader)
for job in jobs:
    # work . . .

I would go with something similar to the second option.

riverscuomo commented 2 years ago

Fascinating. Yeah, I'll go with option two.