python - 无法将参数从 ant build.xml 文件传递​​给 pythonscript。我怎样才能传递值?

标签 python xml build ant

我在运行 build.xml 文件时尝试从我的 ant build.xml 文件运行 python 程序,但我无法将参数传递给 python 脚本

检查给定数字的阶乘的 Python 程序:

事实.py

#!/usr/bin/python

def factorial(num):
    if num == 1:
        return num
    else:
        return num * factorial(num - 1)

 num = int(input('Enter a Number: '))
 if num < 0:
     print 'Factorial cannot be found for negative numbers'
 elif num == 0:
     print 'Factorial of 0 is 1'
 else:
     print ('Factorial of', num, 'is: ', factorial(num))

build.xml 文件如下所示:

<project name="ant_test" default="python" basedir=".">
<target name="python" >
        
        <exec dir="D:\app\python" executable="D:\app\python\python.exe" failonerror="true">
            <arg line="D:\app\ant-workout\fact.py/>
        </exec>
</target>

当运行 build.xml 文件时,它会运行 fact.py python 程序,并且它期望用户输入来检查给定数字的阶乘。

如何将数字从 ant build.xml 文件传递​​给 python 程序

提前致谢!!!!!!

最佳答案

根据documentation

Note that you cannot interact with the forked program, the only way to send input to it is via the input and inputstring attributes. Also note that since Ant 1.6, any attempt to read input in the forked program will receive an EOF (-1). This is a change from Ant 1.5, where such an attempt would block.
(emphasis mine)

那你能做什么?提供以下之一:

input
A file from which the executed command's standard input is taken. This attribute is mutually exclusive with the inputstring attribute.

inputstring
A string which serves as the input stream for the executed command. This attribute is mutually exclusive with the input attribute.


例子:

<project name="ant_test" default="python" basedir=".">
<target name="python" >
        
        <exec dir="D:\app\python" executable="D:\app\python\python.exe" 
                                  failonerror="true" 
                                  inputstring="42">
            <arg line="D:\app\ant-workout\fact.py/>
        </exec>
</target>

关于python - 无法将参数从 ant build.xml 文件传递​​给 pythonscript。我怎样才能传递值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63897693/

相关文章:

python - bool 掩码,如果 df 的时间戳与第二个 df 的两个时间点 - python

python - 谷歌地球引擎: Exporting MODIS images from GEE to AWS S3 bucket

ruby - 使用 Nokogiri 构建空白 XML 标签?

java - 什么是 org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER 以及如何使它在 IntelliJ 中工作?

android aapt 不适用于 arch linux

python - 在 zip 文件中递归列出所有目录,无需在 python 中解压

python - 不同步长的欧拉方法。如何更改算法代码以适应不同的步长值?

c# - 将字符串列表或字符串数​​组传递到 Unity 注入(inject)构造函数(基于配置)

java - 根据字符串更改Textview

ios - 在 Xcode 中构建时添加变量