2015-07-10 5 views
0

Я бегу Ubuntu 14.04 LTS, и у меня есть набор сценариев Python, где:Как автоматизировать выполнение нескольких взаимозависимых сценариев Python

  • первый сценарий приобретает ввод с клавиатуры, выполняет изменения к нему , и сохраняет измененные выходные в файле
  • второго сценарий считывает вывод, сгенерированный первым скриптом и получает (дополнительный) ввод с клавиатурой, выполняет манипуляцию, и сохраняют некоторый результат во втором файле
  • т.д.

Ради конкретности, допустим, у меня есть два сценария Python 1_script.py и 2_script.py, исходные коды которого приведены ниже (в конце этого сообщения).

Я хотел бы знать, как я мог бы запустить одну команду в терминале, который выполняет все следующие действия:

  1. выполняет 1_script.py
  2. обеспечивает «привет», когда 1_script. ру запрашивает ввод с клавиатуры
  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