I'm fairly new to Python - though not new to programming by any means. I have this Python script and it works perfect for my needs but being new(er) to Python I'm just looking to see if there are any general things I could be doing better or 'more Python-esque'. I recently refactored all of this into what you see now - it used to be one long script. It's now separated out into logical methods. But I know all too well that code can ALWAYS be cleaner. So, any tips or things that could be improved?
Side note: I changed the file paths/Twitter authentication tokens/etc for security purposes but the rest of the code is untouched.
import tweepy import pytz import os idfile = '/pathtofile/tweet_lastID.txt' def get_tweets(): ''' Sets up and returns the twitter api. ''' consumerKey = 'consumerKey' consumerKeySecret = 'consumerKeySecret' accessToken = 'accessToken' accessTokenSecret = 'accessTokenSecret' auth = tweepy.OAuthHandler(consumerKey, consumerKeySecret) auth.set_access_token(accessToken, accessTokenSecret) return tweepy.API(auth).user_timeline('mytwitterhandle', since_id=get_last_id_from_file()) def get_last_id_from_file(): ''' Gets the id of the last downloaded tweet from the id file. ''' with open(idfile, 'r') as f: return f.readline().rstrip() def update_last_id(tweets): ''' Updates the id file with the latest id. ''' if not tweets: return for tweet in reversed(tweets): latestID = tweet.id_str with open(idfile, 'w') as f: f.write(latestID + '\n') def write_tweets_to_file(tweets): ''' Writes the given tweets to the backup file. ''' if not tweets: return doc_path = '/pathtofile/tweets_backup.txt' with open(doc_path, 'a') as f: for tweet in reversed(tweets): f.write('\n'.join(tweet).encode('utf8')) def format_date(date): ''' Formats a given date to the format '%-d %b %Y at %-I:%M%p'. ''' datefmt = '%-d %b %Y at %-I:%M%p' homeTZ = pytz.timezone('US/Central') utc = pytz.utc ts = utc.localize(date).astimezone(homeTZ) timeString = ts.strftime(datefmt) timeOfDay = timeString[len(timeString)-2:] timeString = timeString[:-2] return timeString + timeOfDay[:-1].lower() def make_tweet(tweet): ''' Returns a formatted tweet to be written to the backup file from the given tweet data. ''' return [format_date(tweet.created_at), tweet.text, '----------\n'] if __name__ == '__main__': tweets = get_tweets() new_tweets =  for tweet in tweets: new_tweets.extend([make_tweet(tweet)]) write_tweets_to_file(new_tweets) update_last_id(tweets)