1

I have this code called CountDown Program. What my program does is:

  1. Count down from 60.

  2. Play a sound file (alarm.wav) when the countdown reaches zero. This means the code needs to do the following:

    • Pause for one second in between displaying each number in the countdown by calling time.sleep().

    • Call subprocess.Popen() to open the sound file with the default application.

Here's the code for it:

import time , subprocess,os
from subprocess import call

def countDown(): for i in range (10): print(i, end='') time.sleep(1)

countDown() subprocess.call([ 'bomb.mp3'])

When I run this I get:

Error: File not Found

I tried with:

subprocess.call([ 'home/username/bomb.mp3']) # location specified

But I got:

Error: Permission denied

Then, I tried with Popen:

subprocess.Popen(['start', 'bomb.mp3'], shell=True)

But I got:

Error: 0123456789bomb.mp3: 1: start: not found

I am not quite sure why this code does not work on Ubuntu, because it works fine on Windows.

What I can do for this? Is there any method to open mp3 files using Python on Ubuntu 20.04 LTS or am I missing out any argument?

Thanks in advance.

  • To identify how to start a shell program, first try to do is in terminal. Once OK, put the right command in your python program. Maybe this thread will try and help you identify the correct way to go. – Marc Vanhoomissen Oct 06 '20 at 07:43

1 Answers1

3

Use:

subprocess.call(['xdg-open', '/home/username/bomb.mp3'])

bomb.mp3 (or alarm.wav) is not a program or script you want to run, but a file you want to open. As your goal appears to be to open it in whatever graphical application is currently registered to handle it--which is what at least one of the earlier versions of your script did when run in Windows--you should use the xdg-open command.

Besides using xdg-open, I've also added a leading / to the front of the path, without which it is resolved relative to the current directory (which would presumably fail except when the current directory is /).


The first version of your program, or something similar to it, may have worked on Windows because, on Windows, non-executable files can be run as commands. This causes them to be open with whatever program their file type is associated with. Most operating systems and most shells do not behave this way. Windows also provides a command for doing this (with slightly different semantics) called start. But other operating systems, such as Ubuntu, don't have start. So when you tried that on Ubuntu, it didn't work either.

What Ubuntu has is xdg-open. Ubuntu is not alone here--xdg-open doesn't work in Windows or macOS, but it does work on systems with graphical desktop environments that follow the freedesktop.org standards. This includes just about any GNU/Linux system (and some other Unix-like operating systems) if the user has a graphical desktop environment installed.

For the xdg-open command to actually succeed, the user must be logged in to a graphical session.

(On macOS, you'd use open.)

Eliah Kagan
  • 117,780