C#调用Python程序传参数获得返回值

作者:小龙狗 时间:2023-10-11 06:49:52 

说明

C# 调用 Python 程序有多种方式,本篇用的是第 4 种:

  • nuget的ironPython;

  • 用 c/c++ 调用python,再封装成库文件,c# 调用;

  • c# 命令行调用.py文件执行;

  • python 程序制作成 .exe 可执行文件,c# 使用命令行进行传参取返回值。

1. Python 脚本

先建个测试脚本 d://Test/EchoHi.py 代码如下:

import sys
def EchoHi(a):
   return ("Hello, " + a)
if __name__ == "__main__":
   # print('参数列表:', str(sys.argv))
   print(EchoHi(sys.argv[1]))

测试一哈

D:\Test>python EchoHi.py Mr.Tree
Hello, Mr.Tree

2. 打包成Windows可执行文件

首先安装给python打包的python包

D:\Test>pip install pyinstaller

执行打包命令,看输出

D:\Test>pyinstaller -F EchoHi.py

21185 INFO: Writing RT_ICON 7 resource with 1128 bytes
21192 INFO: Updating manifest in D:\Test\build\EchoHi\run.exe.0u78g5s3
21444 INFO: Updating resource type 24 name 1 language 0
21447 INFO: Appending archive to EXE D:\Test\dist\EchoHi.exe
21634 INFO: Building EXE from EXE-00.toc completed successfully.

这里有生成的可执行文件的位置,进入可执行文件的目录测试

D:\Test\dist>EchoHi.exe Mr.Tree
Hello, Mr.Tree

3. C# 程序

CallCmd.cs 代码如下

using System;
class Test
{
   public static void Main(String[] args)
   {
     string cmdpath = "d://Test/dist/EchoHi.exe";
     string arguments = "Mr.Cmd";
     Console.WriteLine(CallCMD(cmdpath, arguments));

}
   public static string CallCMD(string _command, string _arguments){
     System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(_command, _arguments);
     psi.CreateNoWindow = true;
     psi.RedirectStandardOutput = true;
     psi.UseShellExecute = false;
     System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
     return(p.StandardOutput.ReadToEnd());
   }
}

特别需要注意的是:

命令参数是 arguments 内不能有多余空格,因为每个空格都会被识别为分割;
还要注意加一层转义,假执行命令为 EchoHi.exe Mr.\"Tree\" (Tree加了双引号)时,定义就应该为

string arguments = "\\\"Mr.Cmd\\\"";

此后编译运行即可。

4. 参考

[1] https://blog.csdn.net/qq_42063091/article/details/82418630

来源:https://blog.csdn.net/ShyLoneGirl/article/details/114171320

标签:C#,调用,Python
0
投稿

猜你喜欢

  • vue日期时间工具类详解

    2024-06-07 16:06:22
  • CSS网页布局编码小技巧整理

    2009-12-30 16:50:00
  • virtualenv实现多个版本Python共存

    2021-11-27 00:50:44
  • python对象及面向对象技术详解

    2023-05-14 00:04:16
  • Golang操作Kafka的实现示例

    2024-05-22 10:18:48
  • 解决Python一行输出不显示的问题

    2021-05-19 19:21:46
  • Python生成随机MAC地址

    2023-04-09 18:05:57
  • Bootstrap Metronic完全响应式管理模板之菜单栏学习笔记

    2023-08-17 10:58:20
  • oracle 函数

    2010-07-23 13:06:00
  • Gregarius中文日期格式问题解决办法

    2023-11-18 09:51:00
  • Pycharm如何运行.py文件的方法步骤

    2023-03-13 12:32:37
  • MySQL存储过程例子(包含事务,输出参数,嵌套调用)

    2024-01-17 06:21:54
  • Python 中的异步 for 循环示例详解

    2021-07-09 10:27:30
  • JavaScript:ES2019 的新特性(译)

    2024-04-10 16:16:57
  • Python子类继承父类构造函数详解

    2023-02-27 09:13:03
  • PHP asXML()函数讲解

    2023-06-08 14:04:37
  • tkinter自定义下拉多选框问题

    2022-09-27 06:40:00
  • Python Multiprocessing多进程 使用tqdm显示进度条的实现

    2021-04-03 19:15:08
  • python 图像处理画一个正弦函数代码实例

    2021-08-10 22:17:51
  • Python基础学习之基本数据结构详解【数字、字符串、列表、元组、集合、字典】

    2021-09-16 17:17:44
  • asp之家 网络编程 m.aspxhome.com