如何在命令提示符下存储python程序的输出?

2024-05-12 22:59:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我将运行时变量从命令提示符传递到python以在那里执行(im=npython)。现在我想存储python程序的结果,并将这些结果返回到命令提示符中。样品如下

set input=
set /P input=Enter Layer Name:%=%
C:\Python27\python.exe F:\xampp\htdocs\flood_publish\projection_raster.py %input%

我将用户输入字符串从上面的命令提示符传递给python程序。 如何将python程序的结果返回到命令提示符中


Tags: name程序layerinput样品exepython27enter
3条回答

假设它是一个bash脚本,从中调用python函数,应该按如下方式执行:

function callPython()
{
    local pythonResult=<code for calling python>
    echo $pythonResult
}

local pythonReturnOutput = $(callPython)

现在可以使用pythonReturnOutput。在

如果不使用bash,shell脚本,这个答案可能不正确。但是,我强烈建议您准备好一个脚本,如果还没有的话。在

这要视情况而定。如果Python代码正在退出一个值(使用exit(<returncode>)),则可以从%ERRORLEVEL%环境变量中检索该值。在

<yourpythoncommand>
echo Return code: %ERRORLEVEL%

如果希望捕获和处理标准输出,则需要使用FOR循环:

^{pr2}$

现在,如果不想逐行处理输出,最简单的方法是将输出重定向到临时文件。在

<yourpythoncommand> >%TEMP%\mytempfile.txt
<do something with %TEMP%\mytempfile.txt>
del %TEMP%\mytempfile.txt

如果有帮助,请告诉我。在

python文件(c.py)代码:

import sys

print('You passed ',sys.argv[1])

Windows批处理代码(a.bat):

^{pr2}$

输出批次代码:

C:\Users\dinesh_pundkar\Desktop>a.bat
Enter Layer Name:Dinesh
You passed  Dinesh
C:\Users\dinesh_pundkar\Desktop>

相关问题 更多 >