Reputation: 25
import random
import winsound
songsArray = []
with open("test.txt") as f:
for line in f:
songsArray.append(line)
songAmount = len(songsArray)
selectedSong = songsArray[2]
print (selectedSong)
winsound.PlaySound(songsArray[2], winsound.SND_ALIAS)
In this code, I'm trying to append each line of a text file into an array which seems to work giving the right output if I print the position in the array but when trying to use it with winsound
it doesn't work with any elements except the last one to be appended.
Does anyone know how to fix this?
Currently, in the array, there are 3 items and when trying to play any other item except the last one it just does a beep.
Upvotes: 1
Views: 137
Reputation: 1474
When Python iterates through the lines in the a file, the newline characters are included in each line. There are a few ways to work with this, here is one:
import random
import winsound
with open("test.txt") as f:
songsArray = f.read().splitlines()
songAmount = len(songsArray)
selectedSong = songsArray[2]
print (selectedSong)
winsound.PlaySound(songsArray[2], winsound.SND_ALIAS)
Upvotes: 1