-2

I need to write a python script to automate some repetitive Linux commands. (Given to me by someone using mac) The commands are mostly mkdir and a htk speech recognition kit commands. I need to execute these commands on cygwin. I tried searching for tutorials on how to do this but don't think I found a right one yet. I'm pretty new to python.

AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
Hacker77
  • 9
  • 2
  • What have you tried? [Did you go here (Python Docs)?](http://docs.python.org/2/reference/) – SomeShinyObject Mar 29 '13 at 01:26
  • Hi, yes I did. I can code in basic python. But I'm not sure how to execute linux commands in cygwin – Hacker77 Mar 29 '13 at 01:28
  • 1
    Related: http://stackoverflow.com/questions/7513133/run-bash-command-in-cygwin-from-another-application – SomeShinyObject Mar 29 '13 at 01:33
  • Are you running cygwin Python, or native Windows python? (Also, I assume you mean you want to execute some cygwin commands, not linux commands. Unless you're using cygwin to ssh to some remote linux box or something?) – abarnert Mar 29 '13 at 01:42
  • Hi abarnert, I'm running native Windows Python and yes, I want to execute cygwin commands – Hacker77 Mar 29 '13 at 02:01
  • @Christopher, I'm not sure what the bat file is on the link you posted. Can I do it without the bat file? Sorry, I'm a teen and I'm new =) – Hacker77 Mar 29 '13 at 02:04

1 Answers1

2

When I use python as a shell replacement, my import section usually looks like this:

from os import mkdir, chdir
from shutil import move, copy, rmtree, copytree
from subprocess import call

This gives me the ability to move and copy files and directories as well as make new directories and delete directories. If you want to call programs on the shell instead of with the python functions, use call from the subprocess module.

# To run the program foo that takes an option and two arguments
# Equivalent to "foo -d bar baz" directly in the shell
call(['foo', '-d', 'bar', 'baz'])

You would use call for the htk stuff.

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
  • Thank you so much! This was what I was exactly looking for! If possible, Any reference to where you learnt this would be appreciated, or else, it's alright! =) – Hacker77 Mar 29 '13 at 02:51
  • [shutil](http://docs.python.org/2/library/shutil.html), [os](http://docs.python.org/2/library/os.html), and [subprocess](http://docs.python.org/2/library/subprocess.html) – SethMMorton Mar 29 '13 at 04:02