Skip to content

Instantly share code, notes, and snippets.

@fastdaima
Created November 7, 2021 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fastdaima/4b1093d1ecbfa3224678c23c077f37fa to your computer and use it in GitHub Desktop.
Save fastdaima/4b1093d1ecbfa3224678c23c077f37fa to your computer and use it in GitHub Desktop.
pathlib_path.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "pathlib_path.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyPGzOqbZgZcj0PjzSRzTU+H",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/fastdaima/4b1093d1ecbfa3224678c23c077f37fa/pathlib_path.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2GGmWRFohbBc",
"outputId": "6bafdb17-7eed-40fd-f807-edec041ac819"
},
"source": [
"!pip install fastai -Uq"
],
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[K |████████████████████████████████| 189 kB 5.4 MB/s \n",
"\u001b[K |████████████████████████████████| 56 kB 4.5 MB/s \n",
"\u001b[?25h"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "IB6moqcxhe2U"
},
"source": [
"from fastai.vision.all import *"
],
"execution_count": 11,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "zkWH7vSphCby"
},
"source": [
"## Issue 1"
]
},
{
"cell_type": "code",
"metadata": {
"id": "6YxWFHVqf2eU"
},
"source": [
"from pathlib import Path"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "08Dgpr4HgCOX"
},
"source": [
"path = Path('bears')"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2MmcC8BRgI6T",
"outputId": "fe241c79-ce93-4a40-eb69-7e3b6945576c"
},
"source": [
"path"
],
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"PosixPath('bears')"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1MdbE-gUgJu0",
"outputId": "78c6e6a8-b953-42e6-9c75-3ab4d44fec4d"
},
"source": [
"%ls"
],
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[0m\u001b[01;34msample_data\u001b[0m/\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FlJIWd9VgO71",
"outputId": "d9e3bb40-ad48-4d53-8d06-18b353d7181a"
},
"source": [
"path.exists()"
],
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Eh0lhxpbgfcN"
},
"source": [
"`Path('dir_name')` only creates posixpath object, it will not create directory "
]
},
{
"cell_type": "code",
"metadata": {
"id": "K2Tl7iMHgZeI"
},
"source": [
"if not path.exists(): path.mkdir()"
],
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2CUwLEu3g2Jk",
"outputId": "91bf8141-0677-4f12-d1ac-1a7a5159aeea"
},
"source": [
"%ls"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[0m\u001b[01;34mbears\u001b[0m/ \u001b[01;34msample_data\u001b[0m/\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xIs-CUp5g3PM",
"outputId": "4dfd5ee4-3963-488f-c076-139029340fe2"
},
"source": [
"path.exists()"
],
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 9
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "V6aQvik0hx8_"
},
"source": [
"## Issue 2"
]
},
{
"cell_type": "code",
"metadata": {
"id": "DJ9u0Vl1g5i7"
},
"source": [
"??download_images"
],
"execution_count": 12,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "2PLK_q-rh0Wl"
},
"source": [
"def download_images(dest, url_file=None, urls=None, max_pics=1000, n_workers=8, timeout=4, preserve_filename=False):\n",
" \"Download images listed in text file `url_file` to path `dest`, at most `max_pics`\"\n",
" if urls is None: urls = url_file.read_text().strip().split(\"\\n\")[:max_pics]\n",
" dest = Path(dest)\n",
" dest.mkdir(exist_ok=True)\n",
" parallel(partial(_download_image_inner, dest, timeout=timeout, preserve_filename=preserve_filename),\n",
" list(enumerate(urls)), n_workers=n_workers, threadpool=True)"
],
"execution_count": 13,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "jXXMtSa-h8Tl"
},
"source": [
"* On download image function, line 5 the directory will be created if destination folder is not present\n",
"\n",
"* so eventhough when you comment the mkdir line, on download_images function, the destination path is created.\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "eeCFXiKRh5YI"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment