如何使用python json运行可执行文件

2024-07-02 10:46:36 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有这个可执行的二进制文件,可以通过终端运行 并通过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是我的可执行文件名,“chmate_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文件


Tags: 文件代码importjsontruedatashellfile
1条回答
网友
1楼 · 发布于 2024-07-02 10:46:36

像这样的

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()

相关问题 更多 >