{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2021-08-09T02:13:11.211131Z", "start_time": "2021-08-09T02:13:11.200197Z" } }, "outputs": [ { "data": { "text/plain": [ "'3.6.9'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from platform import python_version\n", "\n", "python_version()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2021-08-09T02:13:11.450083Z", "start_time": "2021-08-09T02:13:11.441777Z" } }, "outputs": [], "source": [ "import os\n", "import shutil" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2021-08-08T23:02:31.595588Z", "start_time": "2021-08-08T23:02:31.592639Z" } }, "source": [ "## Create directory if not exists" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-08-09T01:56:11.731196Z", "start_time": "2021-08-09T01:56:11.720779Z" } }, "outputs": [], "source": [ "new_directory_path = \"/path/to/new/directory\"\n", "\n", "if not os.path.exists(new_directory_path):\n", " os.mkdir(new_directory_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mkdir -p behaviour" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-08-09T02:12:06.722971Z", "start_time": "2021-08-09T02:12:06.690252Z" } }, "outputs": [], "source": [ "new_directory_path = \"/path/to/new/directory\"\n", "\n", "if not os.path.exists(new_directory_path):\n", " os.makedirs(new_directory_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Delete directory\n", "\n", "> Including subdirectories, if they exist!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "foo\n", "└── bar\n", " ├── baz\n", " │   └── some-other-file.txt\n", " └── some-file.txt\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import shutil\n", "\n", "directory_path_to_remove = \"/path/to/foo/\"\n", "\n", "if os.path.exists(directory_path_to_remove):\n", " shutil.rmtree(directory_path_to_remove)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }