Offset
Offset

Reputation: 649

Subprocess call bash script

Im trying to call a python class which contains a subprocess call for a bashscript.

def downloadURL(self,address):

        call(['bash youtube2mp3_2.sh', str(address)],shell=True)

This is the bash script:

#!/bin/bash 
# Script for grabbing the audio of youtube videos as mp3. 
address=$1
user_name=$2 # todo

path_music=/home/pi/music_loaded

echo  Address : $address

#title=$(youtube-dl --get-title $address)
youtube-dl --no-playlist  -x --audio-format mp3 -o $path_music/'%(title)s.%          (ext)s' $address
echo ----Download finshed----

When calling the method downloadURl I pass a static youtube link. Printing him in the python method returns it correctly.

But the echo in the script return "" in consonsole. So I think the Argument which I`am trying to pass is not passed to the script.

Does anyone have a Idea?

Upvotes: 0

Views: 1504

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140256

either you pass the arguments as a string

def downloadURL(self,address):
        call('bash youtube2mp3_2.sh '+address)

or as a list

def downloadURL(self,address):
        call(['bash','youtube2mp3_2.sh',address])

both work, but the latter is better so space chars are quoted if necessary.

(BTW since you prefix your command by bash you don't need the shell=True)

Third alternative: let the system choose which interpretor to use according to the shebang:

def downloadURL(self,address):
        call(['youtube2mp3_2.sh',address],shell=True)

PS: The args you're passing ['bash youtube2mp3_2.sh', str(address)] make call add quotes to the first one because it contains spaces, well I don't know what the hell happens on Linux, but I tested on windows and I got a "syntax error", nothing got executed. (Works with the fixed code, I have MSYS bash in my path)

PS2: why calling such a simple bash script from python. Do it in python:

def downloadURL(self,address):
   path_music="/home/pi/music_loaded"
   rc=call(["youtube-dl","--no-playlist","-x","--audio-format","mp3","-o",os.path.join(path_music,'%(title)s.%          (ext)s'),address])
   if rc==0:
      print("Download OK")

Upvotes: 2

Related Questions