我正在运行Ubuntu 14.04 LTS,我有一个Python脚本集合,其中:
为了具体起见,我们假设我有两个Python脚本 1_script.py 和 2_script.py ,其源代码如下(at这篇文章的结尾)。
我想知道如何在终端中运行单个命令,执行以下所有操作:
我很感激您在这方面可能提出的任何建议。
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
答案 0 :(得分: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"