How do you execute external programs from Python?

Carl phleum_nospam at chello.se
Thu Nov 25 13:39:10 EST 2004


Hi,

I have this little code snippet that I use for recording audio streams. My
problem is that I want to schedule my recordings with crontab. This does
not work, however. I cannot figure out why; my code works fine when run
manually from the command prompt. 

On final thing: What would be the best way to turn off the recording?

Can someone help me?

Carl


Here is my code:

import time
import os
import shutil

URL = "http://www.jazzandblues.org/listen/links/kkjz1.ram"

def RecordURL(what = "KKJZ"):
    str_mplayer = "mplayer -playlist " + URL
    str_mplayer += " -ao pcm -aofile "
    str_mplayer += what + ".wav" + " -vc dummy -vo null"
    os.system(str_mplayer)

def CreateOGG(what = "KKJZ"):
    ogg_file = what + ".ogg"
    str_oggenc = "oggenc " + what + ".wav"
    os.system(str_oggenc)

def CreateMP3(what = "KKJZ"):
    mp3_file = what + ".mp3"
    str_lame = "lame " + what + ".wav " + mp3_file
    os.system(str_lame)

def DeleteWAV(what = "KKJZ"):
    wav_file = what + ".wav"
    str_rm = "rm " + wav_file
    os.system(str_rm)

def DateTag(what = "KKJZ"):
    local_time = time.asctime(time.localtime())
    for local_file in [ what + ".ogg", what + ".mp3" ]:
        length = len(local_file)
        local_file_copy = local_file[: length - 4] + "_" + local_time + "."
+ local_file[length - 3 :]
        shutil.copy(local_file, local_file_copy)
            
RecordURL()
## CreateOGG()
## CreateMP3()
## DeleteWAV()
## DateTag()

Here is my crontab entry:

30 * * * * python -c "import os; os.chdir('/home/alpha/mymusic/P2/Jazz/');
import KKJZ"





More information about the Python-list mailing list