Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running files using random
#1
Hi,

The following code plays a mp3 file from a folder randomly. After few days running, it stops but I just cannot find the problem/cause. What king of checks should I add into the code to trap the culprit line?

TIA

#!/usr/bin/python3

from __future__ import print_function
import random, os
import pygame
import logging

#music files path
path = "/media/usb/"

#get music files
songs = os.listdir(path)

#filter mp3 files
songs = [fi for fi in songs if fi.endswith(".mp3")]

pygame.init()
pygame.mixer.init()
#print(pygame.mixer.get_init())

def play_songs():
    try:
        #pygame.mixer.music.set_volume(0.50)
        filename = random.choice(songs)
        #print('playing now {}'.format(filename))
        pygame.mixer.music.load(path + filename)
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy():
            pygame.time.Clock().tick(10)
    except ValueError:
	    logging.exception("msg from music.py: " +format(ValueError))
        print('Excemption: {}', format(ValueError))

if __name__ == '__main__':
    while True:
        play_songs()
Reply
#2
Just define a main function:
import sys

def main():
    while True:
        play_songs()

if __name__ == '__main__':
    sys.exit(main())
That way you get the return code of the script.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
So, if I understand the code right, it loops regardeless of the error (if any)?
Thank you.
Reply
#4
This is an addition to your code. A wrapper function for the last if block. When it happens to stop again you should see the exit code of the program and you can make assumptions about the reason.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
I see, unfortunatelly, the board is not connected to a monitor. I can only see the errors in the syslog.
Reply
#6
You can just redirect the output to a file and then you can read it.

my_scipt.py > /path/exit_status.txt
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running script on multiple files Afrodizzyjack 1 2,467 May-14-2021, 10:49 PM
Last Post: Yoriz
  Flask files running order TomasAm 0 1,433 Nov-12-2020, 08:22 AM
Last Post: TomasAm
  Random access binary files with mmap - drastically slows with big files danart 1 3,908 Jun-17-2019, 10:45 AM
Last Post: danart
  Multi-processing - problem with running multiple *.py files at the same time Antonio 5 3,746 Sep-12-2018, 01:08 PM
Last Post: volcano63
  Running multiple files Totalnoobwithhelp 3 3,349 Jul-18-2018, 09:22 PM
Last Post: Vysero
  Running a python tool transforming xml files into epub files silfer 7 5,313 May-10-2018, 03:49 PM
Last Post: snippsat
  Running a code through multiple files in a folder Rai 1 2,461 Feb-13-2018, 12:42 PM
Last Post: Larz60+
  [split] running .py files on Windows metulburr 7 5,205 Sep-14-2017, 09:16 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020