2013-03-29 36 views
-2

我需要編寫一個python腳本來自動化一些重複的Linux命令。 (由使用mac的人給我的)命令主要是mkdir和htk語音識別工具包命令。我需要在cygwin上執行這些命令。我試圖尋找如何做到這一點的教程,但不認爲我找到了一個正確的。我對Python很新。需要編寫python腳本才能在cygwin中執行linux命令

+0

你有什麼試過? [你去過這裏(Python文檔)?](http://docs.python.org/2/reference/) – SomeShinyObject

+0

嗨,是的,我做到了。我可以在基本的python中編寫代碼。但我不知道如何在cygwin中執行linux命令 – Hacker77

+1

相關:http://stackoverflow.com/questions/7513133/run-bash-command-in-cygwin-from-another-application – SomeShinyObject

回答

2

當我使用Python作爲外殼更換,我進口部分通常是這樣的:

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

這讓我移動和複製文件和目錄,以及作出新的目錄,刪除目錄的能力。如果您想在shell中調用程序而不是使用python函數,請使用subprocess模塊中的call

# 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']) 

你會使用call爲htk的東西。

+0

非常感謝!這正是我正在尋找的!如果可能的話,任何提及你學習到的地方都會受到讚賞,否則,它是沒問題的! =) – Hacker77

+0

[shutil](http://docs.python.org/2/library/shutil.html),[os](http://docs.python.org/2/library/os.html)和[子](http://docs.python.org/2/library/subprocess.html) – SethMMorton