{ "metadata": { "name": "", "signature": "sha256:32b969a4e6c7c3e26cae81dfc07a38f907135ecba42af43e2cfc7a389c8e7205" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 10: Files" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 1, page no. 372" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fp = open(\"outfile1.txt\", \"w\")\n", "i = int(raw_input(\"Input an integer: \"))\n", "fp.write(str(i))\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Input an integer: 22\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 2, page no. 372" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fp = open(\"outfile2.txt\", \"w\")\n", "fp.write(\"string written to file\\n\")\n", "fp.write(\"c\\n\")\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3, page no. 372" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fp = open(\"outfile1.txt\", \"r\")\n", "l = fp.readline()\n", "print \"The integer in outfile1.txt is \", l" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The integer in outfile1.txt is 22\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.1, page no. 374" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fp = open(\"sturec\", \"w\")\n", "print \"Enter student's name and mark: \"\n", "print \"Use 'q' to stop entry\"\n", "while True:\n", " name = raw_input()\n", " if name == 'q':\n", " break\n", " marks = raw_input()\n", " fp.write(name+','+marks+'\\n')\n", "fp.close()\n", "fp = open(\"sturec\", \"r\")\n", "print \"Name\\tMark\"\n", "print \"------------\"\n", "lines = fp.readlines()\n", "for line in lines:\n", " line = line.split(',')\n", " print line[0], '\\t', line[1]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter student's name and mark: \n", "Use 'q' to stop entry\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "vivek\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "87\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "rajkumar\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "98\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "anand\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "67\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "babu\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "45\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "teja\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "90\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "q\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Name\tMark\n", "------------\n", "vivek \t87\n", "\n", "rajkumar \t98\n", "\n", "anand \t67\n", "\n", "babu \t45\n", "\n", "teja \t90\n", "\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.2, page no. 376" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fp = open(\"str.txt\", \"w\")\n", "print \"Enter characters \"\n", "print \"Use 'q' to stop entry\"\n", "while True:\n", " char = raw_input()\n", " if char == 'q':\n", " break\n", " fp.write(char)\n", "fp.close()\n", "fp = open(\"str.txt\", \"r\")\n", "lines = fp.readlines()\n", "count = 1\n", "string = ''\n", "for line in lines:\n", " print line,\n", " string += line\n", "print \"\\nNumber of characters in the file: \", len(string)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter characters \n", "Use 'q' to stop entry\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "I\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "s\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "a\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "w\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "t\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "h\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "e\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "c\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "a\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "t\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "c\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "l\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "i\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "c\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "k\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "t\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "h\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "e\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ " \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "m\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "o\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "u\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "s\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "e\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ ".\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "q\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "I saw the cat click the mouse. \n", "Number of characters in the file: 30\n" ] } ], "prompt_number": 25 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.3, page no. 378" ] }, { "cell_type": "code", "collapsed": false, "input": [ "fp = open(\"class.rec\", \"w\")\n", "print \"Enter the register number, name and mark: \"\n", "print \"Use 'q' to stop entry\"\n", "while True:\n", " reg_no = raw_input()\n", " if reg_no == 'q':\n", " break\n", " name = raw_input()\n", " marks = raw_input()\n", " fp.write(reg_no+','+name+','+marks+'\\n')\n", "fp.close()\n", "fp = open(\"class.rec\", \"r\")\n", "lines = fp.readlines()\n", "for line in lines:\n", " line = line.split(',')\n", " print line[0], '\\t', line[1], '\\t\\t', line[2]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the register number, name and mark: \n", "Use 'q' to stop entry\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4005\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "babu\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "85\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4007\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "rajkumar\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "98\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4012\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "raju\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "77\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4028\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "teja\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "93\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4029\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "vivek\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "87\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "q\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "4005 \tbabu \t\t85\n", "\n", "4007 \trajkumar \t\t98\n", "\n", "4012 \traju \t\t77\n", "\n", "4028 \tteja \t\t93\n", "\n", "4029 \tvivek \t\t87\n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.4, page no. 381" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "import sys\n", "fp = open(\"class.rec\", \"r\")\n", "fp.seek(0, os.SEEK_END)\n", "size = fp.tell()\n", "print \"The file class.rec is of \", size, \" bytes\"\n", "fp.seek(0)\n", "lines = fp.readlines()\n", "while True:\n", " i = int(raw_input(\"Enter the record no. to read(0 to stop): \"))\n", " if i == 0:\n", " break\n", " fp.seek(0)\n", " counter = 0\n", " for line in lines:\n", " counter += 1\n", " if counter == i:\n", " print line.split(\",\")[0], line.split(\",\")[1], line.split(\",\")[2]\n", " break" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The file class.rec is of 70 bytes\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the record no. to read(0 to stop): 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "4005 babu 85\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the record no. to read(0 to stop): 2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "4007 rajkumar 98\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the record no. to read(0 to stop): 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "4012 raju 77\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the record no. to read(0 to stop): 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "4028 teja 93\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the record no. to read(0 to stop): 0\n" ] } ], "prompt_number": 19 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8, page no. 384" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#The example will remain same as above" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example fwrite.c, page no. 390" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#there is no concept of buffered write. We will do it normally\n", "\n", "fp = open(\"testbuf.txt\", \"w\")\n", "fp.write(\"1. This is fwrite\\n\")\n", "fp.write(\"2. this is write\\n\")\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "#rest of the examples in the chapters deal with command line arguments which is not possible in IPython Notebook." ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 24 } ], "metadata": {} } ] }