Skip to main content

Thread: Howto: Run bash shell commands from within Python


the subprocess module can used call command-line programs within python. however, syntax long-winded , hard remember. convenient simple python script can save time is:

code:
def bash(cmd,cwd=none):      '''runs command in bash shell'''      print(cmd)      retval = subprocess.popen(cmd, shell=true, \          stdout=subprocess.pipe, cwd=cwd).stdout.read().strip('\n').split('\n')      if retval==['']:          return(0)      else:          return(retval)
example uses are:

code:
bash('convert myfile.png myfile.jpg')
and

code:
myfilelist = bash('ls *.jpg',cwd='mydirectory')



Forum The Ubuntu Forum Community Other Discussion and Support Tutorials Outdated Tutorials & Tips Howto: Run bash shell commands from within Python


Ubuntu

Comments

Popular posts from this blog

Joomla site hacked, cant see front and - Joomla! Forum - community, help and support

Christian Home School Programs - Joomla! Forum - community, help and support

Trouble with PF_OutFlag_I_USE_AUDIO and PF_CHECKOUT_LAYER_AUDIO