Skip to main content
 首页 » 编程设计

python之从 Python 运行 Matlab M 函数

2025年05月04日10xing901022

我想从 Python 运行我的 Matlab 函数 (test.m)。我使用 Matlab 的 mcc -m 命令将函数转换为 exe 文件 test.exe;我可以从 Windows 命令提示符运行它 test.exe。

另一方面,当我通过 Python 使用 os.system 和 subprocess.call 运行 exe 文件时,它运行良好:

subprocess.call('C:\Program Files\DVD Maker\DVDMaker.exe',shell=True)

(我的 DVDMaker 打开)

但是当我跑的时候
subprocess.call('C:\...\test.exe',shell=True)

我收到这个: 文件名、目录名或卷标语法不正确。

请您参考如下方法:

您的字符串处理有误。

代替

'C:\...\test.exe' 

要么使用

'C:\\...\\test.exe' 

或

r'C:\...\test.exe' 

甚至

'C:/.../test.exe' 

这也会起作用。

IOW,在您的原始字符串中,\test 部分被识别为 <TAB character> + 'est'。你必须用另一个 \ 引用 \,使用 raw 字符串,它不关心有趣的 \将 \ 替换为 /。