From 540b71fe1079d3d28cdeba91b71c4c2abf59c92d Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Tue, 27 Jan 2015 13:45:34 -0500 Subject: [PATCH] Added files IPython Notebook containing common file snippets. --- core/files.ipynb | 122 +++++++++++++++++++++++++++++++++++++++++++ core/hello_world.txt | 1 + 2 files changed, 123 insertions(+) create mode 100644 core/files.ipynb create mode 100644 core/hello_world.txt diff --git a/core/files.ipynb b/core/files.ipynb new file mode 100644 index 0000000..ab025c3 --- /dev/null +++ b/core/files.ipynb @@ -0,0 +1,122 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:c7a4bcb2e55f64b6c49581b50c190fa5f6742f215ee7c4e3a6d10b11319a56fb" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Files\n", + "\n", + "* Open a File for Reading\n", + "* Read a File\n", + "* Write to a New File" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Open a File for Reading\n", + "\n", + "Open a file in read-only mode:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "path = 'type_util.py'\n", + "f = open(path)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Read a File\n", + "\n", + "Read the entire file with readlines. Iterate over the file lines as you would with a list. rstrip removes the EOL markers." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "for line in f:\n", + " print(line.rstrip())" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "class TypeUtil:\n", + "\n", + " @classmethod\n", + " def is_iterable(cls, obj):\n", + " \"\"\"Determines if obj is iterable.\n", + "\n", + " Useful when writing functions that can accept multiple types of\n", + " input (list, tuple, ndarray, iterator). Pairs well with\n", + " convert_to_list.\n", + " \"\"\"\n", + " try:\n", + " iter(obj)\n", + " return True\n", + " except TypeError:\n", + " return False\n", + "\n", + " @classmethod\n", + " def convert_to_list(cls, obj):\n", + " \"\"\"Converts obj to a list if it is not a list and it is iterable,\n", + " else returns the original obj.\n", + " \"\"\"\n", + " if not isinstance(obj, list) and cls.is_iterable(obj):\n", + " obj = list(obj)\n", + " return obj\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Write to a New File\n", + "\n", + "Create a new file (overwriting any previous file with the same name), write text then close the file:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "path = 'hello_world.txt'\n", + "f = open(path, 'w')\n", + "f.write('hello world!')\n", + "f.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/core/hello_world.txt b/core/hello_world.txt new file mode 100644 index 0000000..bc7774a --- /dev/null +++ b/core/hello_world.txt @@ -0,0 +1 @@ +hello world! \ No newline at end of file