{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Directories and Folder structures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we've started working with files, let's take a moment to learn a little more about how your computer stores your files.\n", "So far, we've noticed a few things about files:\n", "1. They have a **name** (like `temp` or `numbers`)\n", "2. Their name has an **extension** (like `.txt` or `.csv`), which describes the file type\n", "3. They usually have some **contents** (like lines of text, or lists of comma-separated values)\n", "\n", "Files also have a **location** on your computer. \n", "- We say that files are stored in a **folder** or **directory**\n", "- The location of a file is described by its **filepath**\n", "\n", "Files are organized in a tree structure. For example, in our class notes directory we have a structure like:\n", "\n", " introduction-to-python\n", " |__ data\n", " |__ restaurant-names.txt \n", " |__ restaurants.txt\n", " |__ notes\n", " |__ A-Introduction_to_iPython_Notebooks.ipynb \n", " |__ B-Numeric_Expressions.ipynb \n", " |__ C1-Variables.ipynb \n", " |__ ...\n", " |__ binder\n", " |__ ...\n", " |__ ...\n", " \n", " So, within the directory this notebook would be located at `introduction-to-python/notes/L2-folders.ipynb`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Working with the os module" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's understand this a little better. To do so, we'll import the `os` module, a convenient way to work with files from within Python." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import os" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Print the current directory and explore it" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's use `os.getcwd()` to print the current working directory (`cwd`)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Print the working directory\n", "start_directory = os.getcwd()\n", "print(f\"We are now in:\\n{start_directory}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should not be surprised to see that you are sitting in the `introduction-to-python/notes/` folder. \n", "What's here? Let's use `os.listdir()` to list the files in the directory." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "# Let's see what kind of files are in this directory \n", "os.listdir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Are there some notebooks about Strings here?\n", "[f for f in os.listdir() if 'String' in f]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# What kinds of .csv or .txt files are there here? I guess none.\n", "[f for f in os.listdir() if f.endswith(\".csv\") or f.endswith(\".txt\")]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Creating a new directory" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's make a directory for the files we made today using `os.makedirs()`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Create our directory\n", "new_directory = start_directory + \"/practice_files\"\n", "os.makedirs(new_directory)\n", "print(f\"We are making a new directory called:\\n{new_directory}\\n\")\n", "\n", "# Check that it's there\n", "print(\"Did we make it?\")\n", "print('practice_files' in os.listdir())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Changing directories" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's enter (change to) the directory that we made using `os.chdir()`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Move to new directory\n", "os.chdir(new_directory) # Change the directory\n", "print(f\"Ok. We are now in directory:\\n{os.getcwd()}\") # Confirm that we're there" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# And, we can go back:\n", "os.chdir(start_directory) # Change the directory\n", "print(f\"Ok. We are now in directory:\\n{os.getcwd()}\") # Confirm that we're there" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Relative file paths" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One convenient tool that you can use is **relative file paths**. For example, so far we have used the full filepaths to navigate:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(start_directory)\n", "print(new_directory)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, we can also move using filepaths that are defined _relative to our current location_. For example, from our position in `01-Introduction_to_Python`:\n", "- We can easily move down into a folder on the next level using its name, e.g. `practice_files/`\n", "- We can move up one level by using the `../` shorthand" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Where are we again? \n", "print(f\"Ok. We are now in directory: {os.getcwd()}\") " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Go down\n", "os.chdir('practice_files/') # Change the directory\n", "print(f\"Ok. We are now in directory: {os.getcwd()}\") # Confirm that we're there" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Go up\n", "os.chdir('../') # Change the directory\n", "print(f\"Ok. We are now in directory: {os.getcwd()}\") # Confirm that we're there\n", "\n", "# Go up two steps\n", "os.chdir('../../') # Change the directory\n", "print(f\"Ok. We are now in directory: {os.getcwd()}\") # Confirm that we're there" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Go down into the SQl folder\n", "os.chdir('introduction-to-python/data/') # Change the directory\n", "print(f\"Ok. We are now in directory: {os.getcwd()}\") # Confirm that we're there\n", "\n", "# Move up and then down (i.e. move laterally)\n", "os.chdir('../notes/') # Change the directory\n", "print(f\"Ok. We are now in directory: {os.getcwd()}\") # Confirm that we're there" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Moving files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use `os.rename(old_name, new_name)` to rename files. \n", "- Be careful, because this will overwrite any existing files called `new_name`.\n", "- `Rename` can also be used to move files; technically, the full syntax is `os.rename(old_filepath, new_filepath)`.\n", "- However, if the old and new file remain in the same folder, we can rely on relative filepaths and just use `old_name`, `new_name`.\n", "\n", "For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Let's write a silly file\n", "with open(\"temp.txt\", \"w+\") as f:\n", " f.write(\"This is a test file\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(f\"Is 'temp.txt' in the directory? {'temp.txt' in os.listdir()}\")\n", "print(f\"Is 'temporary.txt' in the directory? {'temporary.txt' in os.listdir()}\")\n", "\n", "print(\"\\nRenaming now...\\n\")\n", "os.rename('temp.txt', 'temporary.txt')\n", "\n", "print(f\"Is 'temp.txt' in the directory? {'temp.txt' in os.listdir()}\")\n", "print(f\"Is 'temporary.txt' in the directory? {'temporary.txt' in os.listdir()}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Let's write two more files(\"temp2.txt\" and \"temp3.txt\") and move our temp files from `notes` to `practice_files` using a loop and relative filepaths. Recall that the names of the files (after our renaming) are:\n", "\n", " [\"temporary.txt\", \"temp2.txt\", \"temp3.txt\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Answer**: \n", "\\# Create files\n", "with open(\"temp2.txt\", \"w+\") as f:\n", " f.write(\"This is a test file\")\n", "with open(\"temp3.txt\", \"w+\") as f:\n", " f.write(\"This is a test file\") \n", "\\# Loop through the files we created\n", "for f in [\"temporary.txt\", \"temp2.txt\", \"temp3.txt\"]: \n", " \\# Define the old and new filepaths\n", " old_name = f\n", " new_name = \"practice_files/\" + f \n", " \\ # Move the files\n", " os.rename(old_name, new_name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "# Check our answer:\n", "# Go to practice files\n", "os.chdir(\"practice_files/\")\n", "\n", "# Confirm that we're there\n", "print(\"Ok. We are now in directory:\")\n", "print(os.getcwd())\n", "\n", "# See what's here -> the moved files\n", "print(\"\\nThe contents of this directory are:\")\n", "print(os.listdir())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Removing files\n", "Finally, we can get rid of junk using `os.remove()` and `os.rmdir()`. \n", "\n", "Note:\n", "- You will get an error if you remove a non-empty directory, so typically, you will want to empty a directory before deleting it.\n", "- You can override this using recursive methods that search inside the directory structure and delete its contents for you. **However, this can be dangerous!!!** You should be extremely careful when using these types of commands.\n", "\n", "\n", "Let's remove the files we created in this notebook, and then remove the `practice_files` directory." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for fname in [ 'temporary.txt','temp2.txt', 'temp3.txt']:\n", " try:\n", " os.remove(fname) # Remove each of our files in \"practice_files\" \n", " except:\n", " print(f\"Couldn't find {fname}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "os.chdir('../') # Navigate up to the parent directory\n", "os.rmdir('practice_files/') # Remove the empty \"practice_files\" folder" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are many helpful operations you can perform on files with Python, including: downloading them from the internet, unzipping them, etc. We will see some of these operations in the coming notebooks." ] } ], "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.6" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "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": true } }, "nbformat": 4, "nbformat_minor": 1 }