简体   繁体   中英

TypeError: can only concatenate str (not "NoneType") to str python boto3

I have written down python boto3 script to read json file. While reading the json file, i have added some arguments using argparse. While calling the script, it threw with below error:

here is the code

#!/usr/bin/env python
import json
import sys
import os
import re
import boto3
import argparse

def init():
    global pa
    parser = argparse.ArgumentParser(description='Additional Params.')
    parser.add_argument('--Tier', nargs='?', dest='Tier', required=False, help='Tier')
    parser.add_argument('--BusinessUnit', nargs='?', dest='BusinessUnit', required=False, help='BusinessUnit')
    parser.add_argument('--LineOfBusiness', nargs='?', dest='LineOfBusiness', required=False, help='LineOfBusiness')
    parser.add_argument('--branchName', nargs='?', dest='branchName', required=False, help='branchName')
    parser.add_argument('--ProjectName', nargs='?', dest='ProjectName', required=False, help='ProjectName')
    parser.add_argument('--ContextPath', nargs='?', dest='ContextPath', required=False, help='ContextPath')
    pa = parser.parse_args()


def update_secret():
    init()
    with open('application-' + pa.Tier + '.properties.json') as f:
        baselist = json.load(f)

    client = boto3.client('secretsmanager', region_name='us-east-1')
    config_secret = get_secret()

    for secrets in baselist:
        response = client.update_secret(SecretId=pa.BusinessUnit + "/" + pa.LineOfBusiness + "/" + pa.ProjectName + "/" + pa.branchName + pa.ContextPath + "/" + 'application_' + pa.Tier, SecretString=baselist[secrets][1:])
    print(response)

if __name__ == '__main__':
    update_secret()
[Container] 2020/11/09 20:13:46 Running command python updateSecrets.py --Tier $Tier --BusinessUnit $BusinessUnit --LineOfBusiness $LineOfBusiness --ProjectName ProjectName --ContextPath $ContextPath --branchName $branchName
Traceback (most recent call last):
  File "updateSecrets.py", line 81, in <module>
    update_secret()
  File "updateSecrets.py", line 30, in update_secret
    with open('application-' + pa.Tier + '.properties.json') as f:
TypeError: can only concatenate str (not "NoneType") to str

You are trying to concatenate a string with a variable that has None value

with open('application-' + pa.Tier + '.properties.json') as f:
# pa.Tier returns None here

While passing the command line arguments don't use '$' sign for the value. $ is not required. Below is the minimum code I wrote to test.

#!/usr/bin/env python
import argparse

def init():
    global pa
    parser = argparse.ArgumentParser(description='Additional Params.')
    parser.add_argument('--Tier', nargs='?', dest='Tier', required=False, help='Tier')
    pa = parser.parse_args()


def update_secret():
    init()
    print('application-' + pa.Tier + '.properties.json')

if __name__ == '__main__':
    update_secret()

Run above program with command line argument as below.

python3 test.py --Tier abc

It prints below output

application-abc.properties.json

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