Python having problems writing/reading and testing in a correct format
<p>I’m trying to make a program that will do the following:</p>

<ul>
<li>check if auth_file exists
<ul>
<li>if yes -> read file and try to login using data from that file
              - if data is wrong -> request new data</li>
<li>if no  -> request some data and then create the file and fill it with requested data</li>
</ul></li>
</ul>

<p>So far:</p>

<pre><code>import json
import getpass
import os
import requests
filename = ".auth_data"
auth_file = os.path.realpath(filename)
url = 'http://example.com/api'
headers = {'content-type': 'application/json'}

def load_auth_file():
    try:
        f = open(auth_file, "r")
        auth_data = f.read()
        r = requests.get(url, auth=auth_data, headers=headers)
        if r.reason == 'OK':
            return auth_data
        else:
            print "Incorrect login..."
            req_auth()
    except IOError:
        f = file(auth_file, "w")
        f.write(req_auth())
        f.close()


def req_auth():
    user = str(raw_input('Username: '))
    password = getpass.getpass('Password: ')
    auth_data = (user, password)
    r = requests.get(url, auth=auth_data, headers=headers)
    if r.reason == 'OK':
        return user, password
    elif r.reason == "FORBIDDEN":
        print "Incorrect login information..."
        req_auth()
    return False
</code></pre>

<p>I have the following problems(understanding and applying the correct way):</p>

<ul>
<li>I can't find a correct way of storing the returned data from req_auth() to auth_file in a format that can be read and used in load_auth file</li>
</ul>

<p>PS: Of course I'm a beginner in Python and I'm sure I have missed some key elements here :(</p>
