{ "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": {} } ] }