[英]How to write my build parameter in file as key and value using python
我有一個帶有構建參數“WorkspaceName”的 Jenkins 構建,我有一個 python 腳本,它打印 WorkspaceName,它必須將工作空間名稱寫入像 WorkspaceName="The value provided in Z2E54334C0A5CE23E3E5A584ZDFAB3" 這樣的文件。
import time
import os
wsname = os.getenv("WorkspaceName")
print (wsname)
fo = open("MyParameters.properties", "w+")
fo.write(wsname)
fo.close()
如果我使用字典,它會在這樣的文件中打印{1: 'aaa'}
但我需要 key=value,(WorkspaceName=MyWorkspaceName)。 我是 python 的新手。
這就是dict
默認實現打印的方式。 如果你想要它不同,你必須拉出你想要的部分並相應地格式化。 您可以使用dict.items()
方法迭代鍵和值:
with open('MyParameters.properties', 'w+') as fo:
for key, val in wsname.items():
new_line = f'{key}="{val}"'
# Or using format -> '{0}="{1}"'.format(key, val)
# Writes to open file, adds a newline at the end
print(new_line, file=fo)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.