linux - Problems running terminal command via Python -


i'm working on small project need control console player via python. example command works on linux terminal:

mplayer -loop 0 -playlist <(find "/mnt/music/soundtrack" -type f | egrep -i '(\.mp3|\.wav|\.flac|\.ogg|\.avi|\.flv|\.mpeg|\.mpg)'| sort) 

in python i'm doing following:

command = """mplayer -loop 0 -playlist <(find "/mnt/music/soundtrack" -type f | egrep -i '(\.mp3|\.wav|\.flac|\.ogg|\.avi|\.flv|\.mpeg|\.mpg)'| sort)"""  os.system(command) 

the problem when try using python gives me error when run it:

sh: 1: syntax error: "(" unexpected 

i'm confused here because exact same string. why doesn't second method work?

thanks.

your default user shell bash. python's os.system command calls sh default in linux.

a workaround use subprocess.check_call() , pass shell=true argument tell subprocess execute using default user shell.

import subprocess command = """mplayer -loop 0 -playlist <(find "/mnt/music/soundtrack" -type f | egrep -i '(\.mp3|\.wav|\.flac|\.ogg|\.avi|\.flv|\.mpeg|\.mpg)'| sort)""" subprocess.check_call(command, shell=true) 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -