{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check if a file or folder exists with os.path\n",
    "\n",
    "- [Dan Bader - Python Tutorial: How To Check if a File or Directory Exists](https://www.youtube.com/watch?v=DvZTW5g82pQ)\n",
    "- https://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Check\n",
    "\n",
    "1. `os.path.exists()`\n",
    "2. `open()` + `try..execpt` (IOError / FileNotFoundError)\n",
    "3. `pathlib.Path.exists()`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```py\n",
    "os.path.exists\n",
    "os.path.isdir\n",
    "os.path.isfile\n",
    "os.path.basename\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Use a Glob() to find files recursively in Python?\n",
    "\n",
    "**Python 3.5+**\n",
    "\n",
    "Starting with Python version 3.5, the glob module supports the `**` directive (which is parsed only if you pass recursive flag):\n",
    "\n",
    "```py\n",
    "import glob\n",
    "\n",
    "for filename in glob.iglob('src/**/*.c', recursive=True):\n",
    "    print(filename)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "For cases where matching files beginning with a dot (.); like files in the current directory or hidden files on Unix based system, use the os.walk solution below."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "import fnmatch\n",
    "import os\n",
    "\n",
    "matches = []\n",
    "for root, dirnames, filenames in os.walk('src'):\n",
    "    for filename in fnmatch.filter(filenames, '*.c'):\n",
    "        matches.append(os.path.join(root, filename))\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
