{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Frequently Used Python Commands" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This post is about the Python commands I use or Google frequently." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To find the absolute path of the directory of the current file being executed -" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import os \n", "\n", "abs_path = os.path.dirname(os.path.realpath(\".\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Using shutil" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from shutil import copyfile, copy\n", "\n", "copyfile(src, dst) # To copy a file from source to destination (using shutil)\n", "copy(src, dst) # To copy a file or directory from source to destination (using shutil)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To create a directory if it doesn't exist" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "if not os.path.exists(directory):\n", " os.makedirs(directory)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To write a JSON to a file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "with open('data.json', 'w') as f:\n", " json.dump(data, f)" ] } ], "metadata": { "interpreter": { "hash": "033d5ea8e9748582193a6d8f975af35153e280c1f566336ac6ff582d76ae2a04" }, "kernelspec": { "display_name": "Python 3.6.8 64-bit", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }