{
 "metadata": {
  "name": "",
  "signature": "sha256:60c23a8f50ffb51e0da71a6e8b36475b7941871a6f534003180327a651bb1c1c"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 13: Persistent Data: File Input and Output"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 13.1, page no. 296"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "try:\n",
      "    if(fp):\n",
      "        print \"infile = \", fp\n",
      "        print \"0\"\n",
      "except IOError:\n",
      "    print \"1\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 13.2, page no. 296"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "try:\n",
      "    fp = open(\"students.dat\", \"w\")\n",
      "    if(fp):\n",
      "        print \"infile = \", fp\n",
      "        print \"0\"\n",
      "except IOError:\n",
      "    print \"1\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "infile =  <open file 'students.dat', mode 'w' at 0x2d80a50>\n",
        "0\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 13.3, page no. 299"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "fp = open(\"students.dat\", \"w\")\n",
      "print \"Writing to the file\"\n",
      "print \"===================\"\n",
      "print \"Enter class name: \",\n",
      "data = raw_input()\n",
      "fp.write(data)\n",
      "fp.write(\"\\n\")\n",
      "print \"Enter number of students: \",\n",
      "data = raw_input()\n",
      "fp.write(data)\n",
      "fp.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Writing to the file\n",
        "===================\n",
        "Enter class name: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "C++\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter number of students: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "32\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 13.4, page no. 301"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "fp = open(\"students.dat\", \"w\")\n",
      "print \"Writing to the file\"\n",
      "print \"===================\"\n",
      "print \"Enter class name: \",\n",
      "data = raw_input()\n",
      "fp.write(data)\n",
      "fp.write(\"\\n\")\n",
      "print \"Enter number of students: \",\n",
      "data = raw_input()\n",
      "fp.write(data)\n",
      "fp.close()\n",
      "fp = open(\"students.dat\", \"r\")\n",
      "print \"Reading from the file\"\n",
      "print \"=====================\"\n",
      "lines = fp.readlines()\n",
      "for line in lines:\n",
      "    print line\n",
      "fp.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Writing to the file\n",
        "===================\n",
        "Enter class name: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "C++\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter number of students: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "32\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Reading from the file\n",
        "=====================\n",
        "C++\n",
        "\n",
        "32\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "example 13.5, page no. 303"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "fp = open(\"students.dat\", \"w\")\n",
      "print \"Writing to the file\"\n",
      "print \"===================\"\n",
      "print \"Enter class name: \",\n",
      "data = raw_input()\n",
      "fp.write(data)\n",
      "fp.write(\"\\n\")\n",
      "print \"Enter number of students: \",\n",
      "data = raw_input()\n",
      "fp.write(data)\n",
      "fp.close()\n",
      "fp = open(\"students.dat\", \"r\")\n",
      "print \"Reading from the file\"\n",
      "print \"=====================\"\n",
      "lines = fp.readlines()\n",
      "for line in lines:\n",
      "    print line\n",
      "fp.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Writing to the file\n",
        "===================\n",
        "Enter class name: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "C++\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter number of students: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "32\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Reading from the file\n",
        "=====================\n",
        "C++\n",
        "\n",
        "32\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 13.6, page no. 305"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "fp = open(\"students.dat\", \"w\")\n",
      "print \"Writing to the file\"\n",
      "print \"===================\"\n",
      "print \"Enter class name: \",\n",
      "data = raw_input()\n",
      "fp.write(data)\n",
      "fp.write(\"\\n\")\n",
      "print \"Enter number of students: \",\n",
      "data = raw_input()\n",
      "fp.write(data)\n",
      "fp.close()\n",
      "fp = open(\"students.dat\", \"r\")\n",
      "print \"Reading from the file\"\n",
      "print \"=====================\"\n",
      "while(fp.readline()):\n",
      "    print fp.readline()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Writing to the file\n",
        "===================\n",
        "Enter class name: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "C++\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter number of students: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "32\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Reading from the file\n",
        "=====================\n",
        "32\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}