如何用python多次調(diào)用exe文件運(yùn)行不同的結(jié)果
摘要: 有個(gè)C++項(xiàng)目是讀取配置參數(shù)文件并打印對(duì)應(yīng)的結(jié)果,后來(lái)需要多次修改配置文件并運(yùn)行,于是想到寫(xiě)個(gè)python腳本執(zhí)行這一過(guò)程。
寫(xiě)一個(gè)測(cè)試項(xiàng)目,項(xiàng)目結(jié)構(gòu)如下:
根目錄
main.cpp // C++項(xiàng)目,從file.csv
中讀取配置文件并打印對(duì)應(yīng)的結(jié)果
main.py // 多次循環(huán),修改配置文件file.csv
,運(yùn)行.exe文件并打印
/build/
untitled.exe // C++生成的.exe文件
/data/
file.csv // C++讀取的配置文件
配置文件file.csv
如下
da, 4
db, 1.1
dc, 1.2
C++讀取配置文件測(cè)試代碼main.cpp
如下
#include <iostream> #include <vector> #include <fstream> #include <sstream> #include <map> using namespace std; map<string, double> readKeyValuePairs(const string& fileName) { map<string, double> myMap; ifstream file(fileName); string line; while (getline(file, line)) { stringstream ss(line); string key; double value; getline(ss, key, ','); ss >> value; myMap[key] = value; } return myMap; } int main() { map<string, double> ans = readKeyValuePairs("../data/file.csv"); cout << ans.size() << "," << ans["da"]+ans["db"] << "; " << endl; }
代碼中注意配置文件與生成的.exe文件的相對(duì)位置。其中ans.size()
用于判斷是否正確讀到了數(shù)據(jù)。
下面的代碼用于多次修改配置文件,運(yùn)行.exe文件并打印出.exe文件的運(yùn)行結(jié)果。
import subprocess def run_exe(exe_path): process = subprocess.Popen(exe_path, stdout=subprocess.PIPE, cwd='build') output, error = process.communicate() return output.decode('utf-8') for n in range(5): with open('data/file.csv', mode='w') as txtfile: print(f'da, {n}\ndb, 1.1\ndc, 1.2', file=txtfile) output = run_exe('build/untitled.exe') print(output, end='')
其中cwd
參數(shù)的詳細(xì)解釋見(jiàn) Python cwd (1) -知乎,如果不設(shè)置這個(gè)參數(shù),.exe文件的運(yùn)行目錄默認(rèn)是根目錄,也就是main.cpp
所在的目錄,需要用這個(gè)參數(shù)改成/build/
目錄,也就是untitled.exe
所在的目錄。
python代碼運(yùn)行結(jié)果如下
3,1.1;
3,2.1;
3,3.1;
3,4.1;
3,5.1;
下面的代碼是chatGPT生成的python調(diào)用exe文件的原始代碼
import subprocess def run_exe(exe_path): process = subprocess.Popen(exe_path, stdout=subprocess.PIPE) output, error = process.communicate() return output.decode('utf-8') exe_path = 'your/exe_file.exe' output = run_exe(exe_path) print(output)
到此這篇關(guān)于如何用python多次調(diào)用exe文件運(yùn)行不同的結(jié)果的文章就介紹到這了,更多相關(guān)python多次調(diào)用exe文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
代碼總結(jié)Python2 和 Python3 字符串的區(qū)別
在本篇文章里小編給大家整理的是一篇關(guān)于Python2 和 Python3 字符串的區(qū)別以及實(shí)例代碼,需要的朋友們學(xué)習(xí)下。2020-01-01如何使用python?wasmtime調(diào)用rust生成的wasm庫(kù)
這篇文章主要介紹了如何使用python?wasmtime調(diào)用rust生成的wasm庫(kù),使用python wasmtime來(lái)訪問(wèn)rust庫(kù)的便捷方法,步驟極其簡(jiǎn)練,可以在生產(chǎn)環(huán)境中使用,需要的朋友可以參考下2023-01-01Django ValuesQuerySet轉(zhuǎn)json方式
這篇文章主要介紹了Django ValuesQuerySet轉(zhuǎn)json方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03基于Python下載網(wǎng)絡(luò)圖片方法匯總代碼實(shí)例
這篇文章主要介紹了基于Python下載網(wǎng)絡(luò)圖片方法匯總代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06python使用yield壓平嵌套字典的超簡(jiǎn)單方法
這篇文章主要給大家介紹了關(guān)于python使用yield壓平嵌套字典的超簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11