在Python脚本和os.path.join中使用Jenkins变量/参数
我试图学习如何在Python脚本中使用Jenkins的变量。我已经了解到需要调用变量,但我不确定在使用os.path.join()的情况下如何实现它们 我不是一个开发者;我是一名技术作家。这段代码是别人写的。我只是尝试调整Jenkins脚本,使其参数化,这样我们就不必为每个版本修改Python脚本 我在Jenkins作业中使用内联Jenkins python脚本。詹金斯字符串参数是“BranchID”和“BranchIDShort”。我已经研究了很多关于如何在Python脚本中建立变量的问题,但是对于os.path.join()的情况,我不确定该怎么做 这是原始代码。我添加了从Jenkins参数建立变量的部分,但我不知道如何在os.path.join()函数中使用它们在Python脚本和os.path.join中使用Jenkins变量/参数,python,variables,jenkins,parameters,parameter-passing,Python,Variables,Jenkins,Parameters,Parameter Passing,我试图学习如何在Python脚本中使用Jenkins的变量。我已经了解到需要调用变量,但我不确定在使用os.path.join()的情况下如何实现它们 我不是一个开发者;我是一名技术作家。这段代码是别人写的。我只是尝试调整Jenkins脚本,使其参数化,这样我们就不必为每个版本修改Python脚本 我在Jenkins作业中使用内联Jenkins python脚本。詹金斯字符串参数是“BranchID”和“BranchIDShort”。我已经研究了很多关于如何在Python脚本中建立变量的问题,但
# Delete previous builds.
import os
import shutil
BranchID = os.getenv("BranchID")
BranchIDshort = os.getenv("BranchIDshort")
print "Delete any output from a previous build."
if os.path.exists(os.path.join("C:\\Doc192CS", "Output")):
shutil.rmtree(os.path.join("C:\\Doc192CS", "Output"))
我期望输出如下:c:\Doc192CS\output
恐怕如果我执行以下代码:
if os.path.exists(os.path.join("C:\\Doc",BranchIDshort,"CS", "Output")):
shutil.rmtree(os.path.join("C:\\Doc",BranchIDshort,"CS", "Output"))
我将获得:c:\Doc\192\CS\Output
是否有方法在此上下文中使用BranchIDshort变量来获取输出c:\Doc192CS\output?User@Adonis给出了正确的解决方案作为注释。他是这样说的:
的确,你是对的。您想要做的是:
os.path.exists(os.path.join(“C:\\”,“Doc{}CS.format(BranchIDshort),“Output”))
(简而言之,第二个参数使用格式字符串)
因此,完整的更正代码为:
import os
import shutil
BranchID = os.getenv("BranchID")
BranchIDshort = os.getenv("BranchIDshort")
print "Delete any output from a previous build."
if os.path.exists(os.path.join("C:\\Doc{}CS".format(BranchIDshort), "Output")):
shutil.rmtree(os.path.join("C:\\Doc{}CS".format(BranchIDshort), "Output"))
谢谢你,阿多尼斯 的确,你是对的。您想要做的是:
os.path.exists(os.path.join(“C:\\”,“Doc{}CS.format(BranchIDshort),“Output”))
(简而言之,在第二个参数中使用格式字符串)这应该是一个答案。这正是我所需要的。非常感谢。如果我遇到你本人,让我请你喝一杯。