簡體   English   中英

如何使用python自動給出正在運行的linux程序命令?

[英]How can I automatically give a running linux program commands with python?

我已經在 Linux 操作系統上進行了大量火災動態模擬 (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 需要一個輸入文件。 現在,如果您遍歷文件和文件夾並從中生成選項列表。 您應該能夠像我在下面(未經測試)一樣構建一個 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 但這應該會讓你朝着正確的方向前進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM