如何使用python自动为正在运行的linux程序提供命令?
我已经在Linux操作系统上做了很多Fire Dynamic Simulation(FDS)计算,现在我有数千个文件夹。每个文件夹都包含一些以特定二进制格式保存的.sf数据。NIST(谁开发了FDS)提供了一个名为fds2ascii的程序,可用于将这些.sf数据解码为.csv格式。此fds2ascii程序一次只能对这些数据进行一次解码。我现在正试图编写一个python脚本来自动解码所有这些数据 问题是,启动fds2ascii程序后,它不会立即开始工作。相反,您需要回答程序提出的以下问题:如何使用python自动为正在运行的linux程序提供命令?,python,linux,Python,Linux,我已经在Linux操作系统上做了很多Fire Dynamic Simulation(FDS)计算,现在我有数千个文件夹。每个文件夹都包含一些以特定二进制格式保存的.sf数据。NIST(谁开发了FDS)提供了一个名为fds2ascii的程序,可用于将这些.sf数据解码为.csv格式。此fds2ascii程序一次只能对这些数据进行一次解码。我现在正试图编写一个python脚本来自动解码所有这些数据 问题是,启动fds2ascii程序后,它不会立即开始工作。相反,您需要回答程序提出的以下问题: >
>> fds2ascii
Enter Job ID string (CHID):
bucket_test_1
What type of file to parse?
PL3D file? Enter 1
SLCF file? Enter 2
BNDF file? Enter 3
3
Enter Sampling Factor for Data?
(1 for all data, 2 for every other point, etc.)
1
Limit the domain size? (y or n)
y
Enter min/max x, y and z
-5 5 -5 5 0 1
1 MESH 1, WALL TEMPERATURE
Enter starting and ending time for averaging (s)
35 36
Enter orientation: (plus or minus 1, 2 or 3)
3
Enter number of variables
1
Enter boundary file index for variable 1
1
Enter output file name:
bucket_test_1_fds2ascii.csv
Writing to file... bucket_test_1_fds2ascii.csv
所有数据的答案都是相同的
我知道如何编写python脚本,以便为每个数据逐个运行fds2ascii程序。但我还需要知道如何让python回答fds2ascii程序提出的这些问题,以使脚本正常工作。请注意,应在fds2ascii程序运行时回答这些问题
有什么方法可以实现这一点吗?我的理解是fds2ascii接受一个输入文件。现在,如果您在文件和文件夹中循环,并从中生成选项列表。您应该能够像下面一样构建选项列表(未测试):
来自子流程导入调用
选项列表=['bucket_test_1',3,1',y',1',-5-5 0 1',35 36',3,1,1',output.csv']
def fds2ascii_和_选项(选项列表)
''获取将解析为input.txt文件的选项列表。
此文件将用作fds2ascii的输入。“”
使用open('input.txt','w')作为fds_输入:
对于选项列表中的e:
fds_输入写入(e)
调用('fds2ascii
我没有测试选项列表,但这应该会让您找到正确的方向。如果您事先知道必要的答案,您可以将预期的输入发送到
fds2ascii
的stdin
,请参见此处的shell脚本示例
from subprocess import call
options_list = ['bucket_test_1', 3, 1, 'y', 1, '-5 5 -5 5 0 1', '35 36', 3, 1, 1, 'output.csv']
def fds2ascii_with_options(options_list)
'''Takes a list of options that will be parsed to an input.txt file.
This file will be used as input for fds2ascii.'''
with open('input.txt', 'w') as fds_input:
for e in options_list:
fds_input.write(e)
call('fds2ascii < input.txt')
print(f'Output csv created: {options_list[-1]}')