Reputation: 975
I'm new to python and I'm trying to create a json file named after an environment variable.
The variable is called parent
with the value galaxy
This is what my file looks like.
import json
import sys
import os
Report = os.environ.get('MainReport')
if Report == "Main":
newFile = os.environ.get('parent')
else:
newFile = os.environ.get('child')
#####Additional Logic Here############
json_output = json.dumps(output,indent=4)
with open(newFile, "w") as outfile:
outfile.write(json_output)
print("Output file is generated in the current directory!")
file.close()
When I run the script, it creates the file galaxy
in my current directory as expected.
But I need the file to be named galaxy.json
I've tried
if Report == "Main":
newFile = os.environ.get('parent').json
But it throws this error
AttributeError: 'str' object has no attribute 'json'
Is there a way to append .json
to the output from os.environ.get
Upvotes: 1
Views: 145
Reputation: 782285
Use the concatenation operator to append a string.
if Report == "Main":
newFile = os.environ.get('parent') + ".json"
Upvotes: 5