实例-python调用rust生成的可执行文件.exe的方法

Cargo.toml

[package]
name = "rust-example5"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
peroxide = "0.31.6"
serde = { version = "1.0.133", features = ["derive"] }
serde_json = "1.0.75"

main.rs

#![allow(non_snake_case)]
// #[macro_use]
extern crate peroxide;
// use peroxide::fuga::*;
use std::env;

fn main() {

    let args: Vec<String> = env::args().collect::<Vec<_>>();  // 从cmd中获取输入

    println!("{:?}", args);
    for arg in &args {
        println!("{:?}", arg);
    }

    let x1 = &args[0];
    let x2 = &args[1];
    println!("{:?}{:?}", x1, x2);

    // let x3 = x1.parse::<f64>().unwrap();
    // let x3 = x2.parse::<Vec<f64>>().unwrap();
    // println!("{}",x3);

    
}



example1.py

# -*- coding: utf-8 -*-
"""
Created on Fri Sep  2 13:36:56 2022

@author: Userss
"""
from numpy import *
# import numpy
import json
import os
import sys

# 在python程序中运行带参数的exe文件
# 方法一、os.system()  会保存可执行程序中的打印值和主函数的返回值,且会将执行过程中要打印的内容打印出来
main = r"target\release\rust-example5.exe" # 你要运行的exe文件
params1 = r'11' # 你要传入的参数
# 如果要传入多个参数,就继续定义,然后在os.system里加起来就行
print(main+' '+params1)

r_v = os.system(main+' '+params1)
print (r_v )


example2.py

# -*- coding: utf-8 -*-
"""
Created on Fri Sep  2 13:36:56 2022

@author: Userss
"""

import os
import subprocess  

# python中3种调用可执行文件.exe的方法二
# commands.getstatusoutput()  会保存可执行程序中的打印值和主函数的返回值,但不会将执行过程中要打印的内容打印出来
main = r"target\release\rust-example5.exe"
if os.path.exists(main):  
    rc,out= subprocess.getstatusoutput(main+' '+'111')  
    print (rc)
    # print ('*'*10)
    print ("out=",out)
    print ("type of out=",type(out))

example3.py

# -*- coding: utf-8 -*-
"""
Created on Fri Sep  2 13:36:56 2022

@author: Userss
"""

import os

# python中3种调用可执行文件.exe的方法三
# popen()  会保存可执行程序中的打印值,但不会保存主函数的返回值,也但不会将执行过程中要打印的内容打印出来
main = r"target\release\rust-example5.exe"
f = os.popen(main+' '+'111')    
data = f.readlines()    
f.close()    
print (data)

posted @   Nazorine  阅读(380)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示