2015-07-10 44 views
0

執行我運行Ubuntu 14.04 LTS,我有Python腳本,其中集合:如何自動化的幾個相互依存的Python腳本

  • 第一個腳本獲取鍵盤輸入,進行修改,以使其,並存儲在一個文件中
  • 的修改的輸出的第二腳本讀取由第一腳本和獲取(附加的)鍵盤輸入產生的輸出,在第二文件
  • 等進行操作,並存儲一些結果

爲了具體說明,我們假設我有兩個Python腳本1_script.py2_script.py,其源代碼在下面給出(在本文結尾處)。

我想知道我怎麼能在執行所有以下操作的終端中運行一個命令:

  1. 執行1_script.py
  2. 提供「你好」時1_script。 PY詢問鍵盤輸入
  3. 執行2_script.py
  4. 提供了 '!'當2_script.py詢問鍵盤輸入

我會很感激你可以在這方面提供任何的建議。

1_script.py

""" 
This script: 
    1) prompts the user to enter a string 
    2) performs modifications to the entered string 
    3) stores the modified string into a file 
""" 

# get user input 
user_entry = raw_input('Enter a string: ') 

# perform modifications to the input 
modified_data = user_entry + '...' 

# store the modified input into a file 
f = open('output_from_1_script.txt', 'w') 
f.write(modified_data) 
f.close() 

2_script.py

""" 
Dependencies: 
    1) before executing this script, the script 1_script.py 
     has to have been successfully run 

This script: 
    1) reads the output generated by 1_script.py 
    2) modifies the read data with a user-supplied input 
    3) prints the modified data to the screen 
""" 

# reads the output generated by 1_script.py 
f = open('output_from_1_script.txt', 'r') 
pregenerated_data = f.readline() 
f.close() 

# modifies the read data with a user-supplied input 
user_input = raw_input('Enter an input with which to modify the output generated by 1_script.py: ') 
modified_data = pregenerated_data + user_input 

print modified_data 

回答

1

創建一個目錄,您可以存儲所有文件 您可以使用模塊系統或包括每個函數進同一文件 進入目錄並執行下面定義的mainfile.py

1_script.py

""" 
This script: 
    1) prompts the user to enter a string 
    2) performs modifications to the entered string 
    3) stores the modified string into a file 
""" 
def get_input(): 
    # get user input 
    user_entry = raw_input('Enter a string: ') 

    # perform modifications to the input 
    modified_data = user_entry + '...' 

    # store the modified input into a file 
    f = open('output_from_1_script.txt', 'w') 
    f.write(modified_data) 
    f.close() 

下一個腳本會在接下來的文件

2_script.py

""" 
Dependencies: 
    1) before executing this script, the script 1_script.py 
     has to have been successfully run 

This script: 
    1) reads the output generated by 1_script.py 
    2) modifies the read data with a user-supplied input 
    3) prints the modified data to the screen 
""" 
def post_input(): 
    # reads the output generated by 1_script.py 
    f = open('output_from_1_script.txt', 'r') 
    pregenerated_data = f.readline() 
    f.close() 

    # modifies the read data with a user-supplied input 
    user_input = raw_input('Enter an input with which to modify the output  generated by 1_script.py: ') 
    modified_data = pregenerated_data + user_input 

    print modified_data 

第三個腳本 mainfile.py

from 1_script import get_input 
from 2_script import post_input 

if __name__=='__main__': 
    get_input() 
    post_input() 
    print "success" 
+0

感謝這個建議。 當我讀到你的回覆時,我意識到我沒有清楚地說出我的問題 - 我真正想要的是避免每次都要手動輸入鍵盤輸入,但是,多虧了你的回覆,我才明白我可以修改你的函數'get_input()'和'post_input()',以便每個函數接受一個輸入參數(並在'mainfile.py'中相應地調用它們)。 再次感謝 - 我接受您的答案。 – A1dvnp

+0

@ A1dvnp高興地知道它的幫助 – Ja8zyjits