【问题标题】:how to run an executable with python json如何使用 python json 运行可执行文件
【发布时间】:2021-05-12 23:04:45
【问题描述】:

所以我有这个可以通过终端运行的可执行二进制文件 并通过 python
使用代码

$`python3 shell_c.py`

python文件包含在哪里

 import subprocess

def executable_shell():
    x=subprocess.run('cd build && ./COSMO/methane_c0.outmol.cosmo', shell=True, capture_output=True)
    print(x)
executable_shell()

其中 COSMO 是我的可执行文件名称,“甲烷_c0.outmol”是动态值,应该与作为扩展名的“.cosmo”一起更改)

为了从 JSON 文件中获取这些值,我创建了一个 JSON 文件 有输入

{
    "root_directory": "C:\\Users\\15182\\cosmo theory\\COSMO\\UDbase8",
    "file_name": "methane_c0",
    "file_format": ".cosmo",
    "output1": "N_atoms",
    "output2": "total number of segments"
}

现在剩下的就是将 file_name 和 file_format 的值传递给子进程代码以运行它。 但我不知道该怎么做。

到目前为止我写的代码是基本的

   import json
        with open ("parameters.json") as file:
            data =json.load(file)
        print(type(data))
        pront(data)

我应该怎么做才能将值传递给 python 文件?

【问题讨论】:

    标签: python json linux parsing terminal


    【解决方案1】:

    这样的?

    import json
    import subprocess
    
    with open ("parameters.json") as file:
        data =json.load(file)
    dynamic_file_name = data['file_name']+'.outmol'+data['file_format']
    
    def executable_shell():
        x=subprocess.run('cd build && ./COSMO/'+dynamic_file_name, shell=True, capture_output=True)
        print(x)
    executable_shell()
    

    【讨论】:

      猜你喜欢
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 2018-12-31
      • 2015-07-16
      相关资源
      最近更新 更多