使用 python configparser 读取 AWS 配置文件时遇到问题
Having trouble reading AWS config file with python configparser
我运行aws configure
设置我的访问密钥ID和秘密访问密钥。这些现在存储在 ~/.aws/credentials
中,看起来像:
[default]
aws_access_key_id = ######
aws_secret_access_key = #######
我正在尝试访问我正在使用 configparser 编写的脚本的这些键。这是我的代码:
import configparser
def main():
ACCESS_KEY_ID = ''
ACCESS_SECRET_KEY = ''
config = configparser.RawConfigParser()
print (config.read('~/.aws/credentials')) ## []
print (config.sections()) ## []
ACCESS_KEY_ID = config.get('default', 'aws_access_key_id') ##configparser.NoSectionError: No section: 'default'
print(ACCESS_KEY_ID)
if __name__ == '__main__':
main()
我运行脚本使用python3 script.py
。对这里发生的事情有什么想法吗?似乎 configparser 根本不是 reading/finding 文件。
configparser.read
不会尝试扩展主目录路径中的前导波浪号“~”。
您可以提供相对或绝对路径
config.read('/home/me/.aws/credentials')
path = os.path.join(os.path.expanduser('~'), '.aws/credentials'))
config.read(path)
path = pathlib.PosixPath('~/.aws/credentials')
config.read(path.expanduser())
我运行aws configure
设置我的访问密钥ID和秘密访问密钥。这些现在存储在 ~/.aws/credentials
中,看起来像:
[default]
aws_access_key_id = ######
aws_secret_access_key = #######
我正在尝试访问我正在使用 configparser 编写的脚本的这些键。这是我的代码:
import configparser
def main():
ACCESS_KEY_ID = ''
ACCESS_SECRET_KEY = ''
config = configparser.RawConfigParser()
print (config.read('~/.aws/credentials')) ## []
print (config.sections()) ## []
ACCESS_KEY_ID = config.get('default', 'aws_access_key_id') ##configparser.NoSectionError: No section: 'default'
print(ACCESS_KEY_ID)
if __name__ == '__main__':
main()
我运行脚本使用python3 script.py
。对这里发生的事情有什么想法吗?似乎 configparser 根本不是 reading/finding 文件。
configparser.read
不会尝试扩展主目录路径中的前导波浪号“~”。
您可以提供相对或绝对路径
config.read('/home/me/.aws/credentials')
path = os.path.join(os.path.expanduser('~'), '.aws/credentials'))
config.read(path)
path = pathlib.PosixPath('~/.aws/credentials')
config.read(path.expanduser())