python - 是否可以从 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 需要继续运行。
谢谢你。
解决方案
推荐阅读
- node.js - 使用 jsdom 将 JSON 反序列化为 DOM 时出现 Node.js 内存泄漏
- regex - Column1 中的 SQL REPLACE 和 Column 2 中的 UPDATE 同时
- php - 使用 PHP 将数据发布到 SOAP API
- reminders - 如何以编程方式设置 Bixby 提醒?
- sql - 删除重复项,保留其中一个,分组
- kendo-ui - 更新后未删除 Kendo 数据源离线 __state__ 属性
- javascript - php echo onClick JS 函数(传递字符串):: Uncaught SyntaxError: Unexpected token
- r - 查找以逗号分隔的列中的所有唯一值
- machine-learning - 特征缩放线性回归模型以及它如何影响输出
- python - 如何从大到小列出数字?