1. 程式人生 > 其它 >如何利用Python呼叫帶引數的exe

如何利用Python呼叫帶引數的exe

技術標籤:python

前言

在實際生產生活中,常常用到別人封裝好的exe,如果有一堆檔案要處理,全靠手動輸入的話,豈不是浪費時間。本篇博文將簡單介紹下如何用python呼叫exe。

exe檔案架構

主要用到的有exe和config這兩個檔案,如下圖所示:
在這裡插入圖片描述

程式碼

主要流程就是,先傳參,再呼叫exe

import os
import glob
import subprocess

def polygonize(imagePath, raster_path, forest_shp_path, pwd):
    # pwd = ''
    os.chdir(os.path.realpath(
os.path.join(pwd,'polygonize/'))) polygonize_exe = os.path.realpath(os.path.join(pwd,'polygonize/polygonize0529.exe')) polygonize_path = os.path.realpath(os.path.join(pwd,'polygonize/polygonize.config')) rmHole = "100" simpoly = "4" scale = "3" with open
(polygonize_path,'w') as f_config: f_config.write("--image=" + imagePath+'\n') f_config.write("--edgebuf="+raster_path+'\n') f_config.write("--line="+forest_shp_path+'\n') f_config.write("--rmHole=" + rmHole + '\n') f_config.
write("--simpoly=" + simpoly + '\n') f_config.write("--scale=" + scale) f_config.close() subprocess.call(polygonize_exe)