简体   繁体   中英

How to write my build parameter in file as key and value using python

I have a Jenkins build with a build parameter "WorkspaceName" and I have a python script which prints the WorkspaceName and it has to write the workspace name into a file like WorkspaceName="The value provided in Jenkins build".

 import time
 import os
 wsname = os.getenv("WorkspaceName")
 print (wsname)
 fo = open("MyParameters.properties", "w+")
 fo.write(wsname)
 fo.close()

If I use dictionary, it prints in file like this {1: 'aaa'}

But I need as key=value, (WorkspaceName=MyWorkspaceName). I am new to python.

That's how dict is implemented to print by default. If you want it differently, you'll have to pull out the parts you want and format accordingly. You can iterate both keys and values by using the dict.items() method:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM