{
 "metadata": {
  "name": "",
  "signature": "sha256:be4a857e05d2149d20f6d26486c5784a7f1b2096e43de372a2b4b95639d8b93a"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 5 : Reading and writing files"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5.1, Page No 81"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "poem  = [\"\\n\\tI never saw a man who looked\"]\n",
      "poem.append(\"\\n\\tWith such a wistful eye\")\n",
      "poem.append(\"\\n\\tUpon that little tent of blue\")\n",
      "poem.append(\"\\n\\tWhich prisoners call the sky\")\n",
      "try:\n",
      "    writer = open(\"poem.txt\",\"w\")\n",
      "    if(not isinstance(writer,file)):\n",
      "        print \"Error opening file for output\"\n",
      "    writer.writelines(poem)\n",
      "    writer.close()\n",
      "except IOError:\n",
      "    print \"Error opening file for output\"\n",
      "\"\"\"\n",
      "Out put Text File :- poem.txt\n",
      "\n",
      "    I never saw a man who looked\n",
      "    With such a wistful eye\n",
      "    Upon that little tent of blue\n",
      "    Which prisoners call the sky\n",
      "    The Ballad of Reading Gaol\n",
      "            Oscar Wilde 1898\n",
      "            \n",
      "\"\"\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5.2, Page No 83"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "info = \"\\n\\tThe Ballad of Reading Gaol\"\n",
      "info = info + \"\\n\\t\\t\\tOscar Wilde 1898\" #There is no append for string in python\n",
      "try:\n",
      "    writer = open(\"poem.txt\",\"a\")\n",
      "    if(not isinstance(writer,file)):\n",
      "        print \"Error opening file for output\"\n",
      "    writer.writelines(info)\n",
      "    writer.close()\n",
      "except IOError:\n",
      "    print \"Error opening file for output\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5.3, Page No 84"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "try:\n",
      "    reader = open(\"poem.txt\",\"r\")\n",
      "    if(not isinstance(reader,file)):\n",
      "        print \"Error opening input file\"\n",
      "    i = 0\n",
      "    while(1):\n",
      "        c = reader.read(1)\n",
      "        if c == \"\":\n",
      "            break\n",
      "        else:\n",
      "            print c,\n",
      "            i= i + 1    \n",
      "    reader.close()\n",
      "    print \"\\nIterations: \",i\n",
      "except IOError:\n",
      "    print \"Error opening input file\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "\tI   n e v e r   s a w   a   m a n   w h o   l o o k e d \n",
        "\tW i t h   s u c h   a   w i s t f u l   e y e \n",
        "\tU p o n   t h a t   l i t t l e   t e n t   o f   b l u e \n",
        "\tW h i c h   p r i s o n e r s   c a l l   t h e   s k y \n",
        "\tT h e   B a l l a d   o f   R e a d i n g   G a o l \n",
        "\t\t\tO s c a r   W i l d e   1 8 9 8 \n",
        "Iterations:  164\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5.4, Page No 86"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# this program gives output same as book program but logic is different because some of functions are not available in python\n",
      "RANGE = 12\n",
      "tab = range(RANGE)\n",
      "i = 0\n",
      "j = 1\n",
      "t = \"\"\n",
      "try:\n",
      "    reader = open(\"records.txt\",\"r\")\n",
      "    if(not isinstance(reader,file)):\n",
      "        print \"Error opening input file\"\n",
      "    while(1):\n",
      "        c = reader.read(1)\n",
      "        if c == \"\":\n",
      "            break\n",
      "        else:\n",
      "            t = t + c\n",
      "            if c == '\\t':\n",
      "                tab[i] = t\n",
      "                t = \"\"\n",
      "                i = i + 1\n",
      "            elif c == '\\n':\n",
      "                tab[i] = t\n",
      "                t = \"\"\n",
      "                i = i + 1\n",
      "            elif i == RANGE-1:\n",
      "                tab[i] = t\n",
      "    reader.close()\n",
      "    i= 0;\n",
      "    while i < RANGE:\n",
      "        print \"Record Number: \",j\n",
      "        j = j + 1\n",
      "        print \"Forename: \",tab[i]\n",
      "        i = i + 1\n",
      "        print \"Surname: \",tab[i]\n",
      "        i = i + 1\n",
      "        print \"Department: \",tab[i]\n",
      "        i = i + 1\n",
      "        print \"Telephone: \",tab[i]\n",
      "        i = i + 1\n",
      "except IOError:\n",
      "    print \"Error opening input file\"\n",
      "\n",
      "\"\"\"\n",
      "    INPUT FILE :- records.txt\n",
      "    \n",
      "    John   Smith   Sales   555-1234\n",
      "    Mary   Jones   Wages   555-9876\n",
      "    Paul   Harris  Accts   555-4321\n",
      "    \n",
      "\"\"\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Record Number:  1\n",
        "Forename:  John\t\n",
        "Surname:  Smith\t\n",
        "Department:  Sales\t\n",
        "Telephone:  555-1234\n",
        "\n",
        "Record Number:  2\n",
        "Forename:  Mary\t\n",
        "Surname:  Jones\t\n",
        "Department:  Wages\t\n",
        "Telephone:  555-9876\n",
        "\n",
        "Record Number:  3\n",
        "Forename:  Paul\t\n",
        "Surname:  Harris\t\n",
        "Department:  Accts\t\n",
        "Telephone:  555-4321\n"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5.5, Page No 89"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "isTrue = 1\n",
      "num = 255\n",
      "for i in range(0,40):\n",
      "    print \".\",\n",
      "print  \"Output\"\n",
      "print \"Pi: \",\"3.1415926536\"\n",
      "print isTrue,\":\",bool(isTrue)\n",
      "print num,\":\",\"{:01X}\".format(num)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Output\n",
        "Pi:  3.1415926536\n",
        "1 : True\n",
        "255 : FF\n"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5.6, Page No 91"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "try:\n",
      "    for number in range(1,21):\n",
      "        if number > 4:\n",
      "            raise Exception(number)\n",
      "        else:\n",
      "            print \"Number: \",number\n",
      "except:\n",
      "    print \"Exception at: \",number"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Number:  1\n",
        "Number:  2\n",
        "Number:  3\n",
        "Number:  4\n",
        "Exception at:  5\n"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5.7, Page No 92"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "lang = \"C++\"\n",
      "try:\n",
      "    lang.erase(4,6)\n",
      "except:\n",
      "    print \"Exception\",sys.exc_info()[0]\n",
      "# There is  no support for erse method in python"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Exception <type 'exceptions.AttributeError'>\n"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 5.8, Page No 94"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#there is no replace or resize method in python\n",
      "import sys\n",
      "lang = \"C++\"\n",
      "num = 1000000000\n",
      "try:\n",
      "    lang.replace(lang,\"C\",\"1\")\n",
      "    lang.resize(3*num)\n",
      "    reader = open(\"nonsuch.txt\",\"r\")\n",
      "    print \"Program continues...\"\n",
      "except:\n",
      "    print sys.exc_info()[0]\n",
      "    print \"Program terminated.\"\n",
      "#python dose not support much more exceptions"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<type 'exceptions.TypeError'>\n",
        "Program terminated.\n"
       ]
      }
     ],
     "prompt_number": 29
    }
   ],
   "metadata": {}
  }
 ]
}