首页 > 解决方案 > 是否可以从 python 将数据发送到 bat 文件中的特定函数?

问题描述

我现在在一个 .bat 项目中工作。到目前为止,.bat 中的函数调用 Python 脚本,.py 检查 .c 文件中的一些数据并在 .txt 文件中生成报告,.bat 继续运行。但是我想在运行 .bat 时在控制台中显示来自 .txt 的信息。我的意思是,将信息从 .py 发送回 .bat。我找到了一种将信息从 .py 发送到 .bat 的方法,但这些方法只能从一开始就打开 .bat,而且只会中断函数的进程。

除了已经提到的 .bat 项目之外,我还制作了两个小脚本(.bat 和 .py),它们可以工作。但是这个逻辑不适用于我的主项目,因为在实现的逻辑中,.py 从一开始就重新打开了 .bat。

这是我的两个小型工作脚本的逻辑: .bat 脚本:

@echo off
color 2f

set arg1=%1
set Tool.Python.exe=D:\uti\programs\Python27\python.exe
set Python.File.Path=C:\Users\gml1028\Desktop\sending_args.py

if "%arg1%"=="" (
    goto openPython
    )
    echo My own interface 
    echo Press 1 for showing some numbers
    echo Press 2 for showing some letters
    echo Press 3 for showing letters and numbers
    set /p select=Select: 
    if "%select%"=="1" (
        goto showNumbers
        )
    if "%select%"=="2" (
        goto showLetters
        )
    if "%select%"=="3" (
        goto showAlphanumerics 
        )

:openPython  
call %Tool.Python.Exe% -u %Python.File.Path%
goto :eof

:end 

:showNumbers
    echo 1
    echo 2
    echo 3
    echo 4
    echo The number is %arg1%
    pause
goto :eof

:showLetters 
    echo a
    echo b
    echo c
    echo d
    echo The number is %arg1%
    pause
goto :eof

:showAlphanumerics 
    echo 1a
    echo 2b
    echo 3c
    echo 4d
    echo The number is %arg1%
    pause
goto :eof

.py 脚本:

import os, subprocess, random 

filepath=r'C:\Users\visgmo\Desktop'
executable = os.path.join(filepath, 'sending_args.bat')
p = subprocess.Popen([executable, str(random.randrange(0,20))])

在这种情况下,我不在乎 .bat 是否重新启动,因为我只是在尝试它是否有效。但我在主项目中这样做是因为在调用 .py 文件后,.bat 需要继续运行。

谢谢你。

标签: pythonwindowsbatch-file

解决方案


推荐阅读