title: Handle a 24 hour token from API
body: <p>I'm working with an API that gives me a 24 hours token. Wath I'm doing is saving this token in a json file and in addition the timestamp when I saving it too.</p>
  <p>It can be three possibilities:</p>
  <ul>
  <li>Don't exists the json file. In that case I login and create the json file.</li>
  <li>The file exists but more than 24 hours have passed. In that case I login and create the json file.</li>
  <li>The file exists and less than 24 hours have passed. It's ok, I just select the token from it.</li>
  </ul>
  <p>This is my code right now:</p>
  <pre><code>if not os.path.isfile('temp/24h_session.json'):
      token = self.login()
      data = {'token': token, 'timestamp':dt.timestamp(dt.now())}
      with open(&quot;temp/24h_session.json&quot;, &quot;w&quot;) as f:
          f.write(json.dumps(data))
  else:
      with open('temp/24h_session.json', 'r') as f:
          data = json.load(f)
      if data['timestamp'] &lt;= dt.timestamp(dt.now() - timedelta(hours = 24)):
          token = self.login()
          data = {'token': token, 'timestamp':dt.timestamp(dt.now())}
          with open(&quot;temp/24h_session.json&quot;, &quot;w&quot;) as f:
              f.write(json.dumps(data))
      else:
          token = data['token']
  </code></pre>
  <p>I'm not happy with it, you think its possible to join the first two possibilities to avoid repeat code? I tried with <code>try/except</code> but the result was similar because the problem is when the file exists but the more than 24 hour have passed.</p>
  <p>Thank you very much!</p>