如何使用python自动给正在运行的linux程序命令?

时间:2020-02-11 15:05:31

标签: 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程序运行时应回答这些问题。

有什么办法可以实现这一目标?

1 个答案:

答案 0 :(得分:0)

我的理解是fds2ascii需要输入文件。现在,如果您遍历文件和文件夹并从中生成选项列表。您应该能够构造一个options_list,就像我在下面看到的那样(未经测试):

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

我尚未测试options_list,但这应该可以使您朝正确的方向前进。