Imported all the notebooks
[tm351-notebooks.git] / notebooks / 01. Bootcamp notebooks / 01.5 Python file handling.ipynb
diff --git a/notebooks/01. Bootcamp notebooks/01.5 Python file handling.ipynb b/notebooks/01. Bootcamp notebooks/01.5 Python file handling.ipynb
new file mode 100644 (file)
index 0000000..1a62b56
--- /dev/null
@@ -0,0 +1,174 @@
+{
+ "metadata": {
+  "name": "",
+  "signature": "sha256:38b7b955c3e37383299d12aafd64ad211d1730aaf9ecb1f4f2487bb1ebc19647"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+  {
+   "cells": [
+    {
+     "cell_type": "heading",
+     "level": 1,
+     "metadata": {},
+     "source": [
+      "Loading Files In and Writing Files Out"
+     ]
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "You can load local files into Python by opening a file object and then using the `read()` function:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "!ls data"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "f=open('data/simpleTest.txt')\n",
+      "txt=f.read()\n",
+      "txt"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "You can create and write files by opening a file object with a write status and then using the `write()` function:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "txt = txt + \"\\n\\nHere's another line...\"\n",
+      "fout=open('data/simpleTest_copy.txt','w')\n",
+      "fout.write(txt)\n",
+      "f2=open('data/simpleTest_copy.txt')\n",
+      "f2.read()"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "!ls data"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "Close files by calling `close()` from the file object:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "f.close()\n",
+      "f2.close()\n",
+      "fout.close()"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "You can also use the `with` command to open a file and assign it to a variable. On exiting the with block, the file will automatically be closed."
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "with open('data/simpleTest.txt') as f:\n",
+      "    ls = [nl for nl in f.readlines()]\n"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "You can load files in from a URL using various helper libraries. For example, the `requests` library [[documentation](http://docs.python-requests.org/en/latest/)]:"
+     ]
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "import requests"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "webRequest=requests.get('http://www.open.ac.uk')\n",
+      "webRequest.content"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "So that's it - a quick recap of the key elements of the Python language that we will be assuming (that is, won't be explaining in detail) for much of the course. If Python is completely new to you, you may find it useful to work through the [\"standard\" Python tutorial](https://docs.python.org/3/tutorial/index.html) or the [M269 python tutorial](https://learn2.open.ac.uk/mod/oucontent/view.php?id=397105&section=2.__0)...\n",
+      "\n",
+      "But inspection of the code, and a little bit of experimentation, can also get you a long way..."
+     ]
+    },
+    {
+     "cell_type": "heading",
+     "level": 2,
+     "metadata": {},
+     "source": [
+      "What Next?"
+     ]
+    },
+    {
+     "cell_type": "markdown",
+     "metadata": {},
+     "source": [
+      "That's it for the notebook activities in this week's session.\n",
+      "\n",
+      "See you next week..."
+     ]
+    }
+   ],
+   "metadata": {}
+  }
+ ]
+}
\ No newline at end of file