Python Tips

This is my personal memo about useful notations in python. I have not touched on the basics. This is limited to things that I find useful.

You can use it to create an automatic bot to tweet automatically.


  • The jupyter notebook format file on github is here .

google colaboratory

  • To run it on google colaboratory here 005/005_nb.ipynb)

Author’s environment

ProductName: Mac OS X
ProductName: Mac OS X
ProductVersion: 10.14.6
BuildVersion: 18G95
Python -V
Python 3.5.5 :: Anaconda, Inc.

Install tweepy

To use the Twitter API, you need to install a module called tweepy.

!pip install tweepy

Example code

The following code is for an automatic tweet that will be executed when a certain trigger is actually pulled in a project. The following code is for an automatic tweet that will run when a certain trigger is pulled in a project. /config/login_info.json` as a config file. You will need to create a Twitter developer account to get this information.

import datetime
import sys

import tweepy
import json

import os
import time

import json
import shutil

def tweet(text, image_file_path):

  json_file = ". /conf/login_info.json"
  with open(json_file) as file:
    payload = json.loads(

  consumer_key = payload["Consumer_key"].
  consumer_secret = payload["Consumer_secret"].
  access_token = payload["Access_token"].
  access_secret = payload["Access_secret"]]

  auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  auth.set_access_token(access_token, access_secret)

  api = tweepy.API(auth, wait_on_rate_limit = True)

  # tweet with image.
  # api.update_with_media(status = text, filename = image_file_path)

  # Normal text tweet

if __name__ == '__main__':

  args = sys.argv

  today =
  yyyy = today.strftime('%Y')
  mm = today.strftime('%m')
  dd = today.strftime('%d')
  yyyymmdd = today.strftime('%Y%m%d')

  text = """
[{0} year {1} month {2} day


""".format(yyyy, mm, dd)

  tweet(text=text, image_file_path="")

config file

The config file looks like the following.

    "COMMENT": "login_info.json",
    "Consumer_key": "",
    "Consumer_secret": "",
    "Access_token": "",
    "Access_secret": ""

By creating these files, it is possible to automatically tweet on certain event triggers. I usually do about 10 automatic tweets a day with this.