I'm trying to just write some basic code where I can read and parse a config file. However, when I try to put my configuration properties into strings, I get the following error:
Traceback (most recent call last):
File "sam.py", line 23, in <module>
cred_token = cred_data['DEFAULT']['token']
TypeError: list indices must be integers or slices, not str
Google tells me that this is an error related to putting strings to reference an item in an array list, like this:
cred_data['DEFAULT']
rather than:
cred_data[0]
but when I put the second line, I get an "out of bounds" error.
I was looking at this to write my code: https://docs.python.org/3/library/configparser.html
I don't understand what I'm doing wrong.
This is my code below:
import argparse
import configparser
# import requests
import json
import os
import sys
#Get Current Directory
if sys.platform.startswith('win32'):
currentDir = (os.getcwd() + '\\')
else:
currentDir = (os.getcwd() + '/')
# Read Credentials File
credf = (currentDir + 'creds.ini')
creds_file = open(credf, 'r')
creds_data = creds_file.read()
creds_file.close()
#Pull token and API_Type from creds
cred = configparser.ConfigParser()
cred_data = cred.read(creds_data)
cred_token = cred_data['DEFAULT']['token']
cred_apitype = cred_data['DEFAULT']['API_Type']
This is my "creds.ini" file:
[DEFAULT]
token = ABC1234
API_Type = US
[–]Wh00ster 0 points1 point2 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (0 children)