The previous post was for commands Unix. This post is about the Python commands I use or Google frequently.

  1. To find the absolute path of the directory of the current file being executed -

    abs_path = os.path.dirname(os.path.realpath(file))


  1. To copy a file from source to destination (using shutil)-

    from shutil import copyfile copyfile(src, dst)


  1. To copy a file or directory from source to destination (using shutil)-

    from shutil import copy copy(src, dst)


  1. To create a directory if it doesn’t exist -

    import os if not os.path.exists(directory): os.makedirs(directory)

  2. To write a JSON to a file -

    import json with open(‘data.json’, ‘w’) as f: json.dump(data, f)