I am trying to update the local aws/config file on my mac. I am not able to read or update any contents of the config file.
import configparser
import os
creds_dir = os.path.dirname("~/.aws/config")
config = configparser.RawConfigParser()
if not config.has_section("some"):
config.add_section("some")
# METHOD -1
config.set("some", 'aws_access_key_id', "access_key")
config.set("some", 'aws_secret_access_key', "secret_key")
config.set("some", 'aws_session_token', "token")
config.set("some", 'aws_security_token', "token")
config.write("~/.aws/config")
# METHOD -2
with open('~/.aws/credentials', 'a') as file_out:
file_out.write("[profile]")
file_out.write("aws_access_key_id = aaa")
file_out.write("aws_secret_access_key = bbb")
file_out.write("aws_session_token = cccc")
I am getting an error:
FileNotFoundError: [Errno 2] No such file or directory: '~/.aws/credentials'
I could open the file from my mac terminal and view it.
CodePudding user response:
Change this to be:
creds_file = os.path.expanduser("~/.aws/config")
or
with open(os.path.expanduser("~/.aws/config"), 'a') as file_out:
The ~
expansion is part of the shell normally and so you need to expand it. expanduser
returns a string that has the full path name to the file.