{ "metadata": { "name": "", "signature": "sha256:81ebacbcf46be2a85cdba0d8109c34324595901036a67c9025bbb5cab58aa894" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 7 : Files" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.1, Page No 157" ] }, { "cell_type": "code", "collapsed": false, "input": [ "outfile = open(\"prodfile1.txt\",\"w\")\n", "cProd = raw_input(\"Enter product, (only Enter to exit:\")\n", "while (len(cProd) > 0):\n", " outfile.writelines(cProd)\n", " outfile.writelines(\"\\n\")\n", " cProd = raw_input(\"Enter product, (only Enter to exit:\")\n", "outfile.close()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product, (only Enter to exit:wheel\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product, (only Enter to exit:handlebars\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product, (only Enter to exit:saddle\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product, (only Enter to exit:bike\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product, (only Enter to exit:moped\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product, (only Enter to exit:\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Examle 7.2 Page No 159" ] }, { "cell_type": "code", "collapsed": false, "input": [ "infile = open(\"prodfile1.txt\",\"r\")\n", "while (1):\n", " cProd = infile.readline()\n", " if(cProd):\n", " print cProd\n", " else:\n", " break\n", "infile.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "wheel\n", "\n", "handlebars\n", "\n", "saddle\n", "\n", "bike\n", "\n", "moped\n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.3, Page No 160" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cProd = raw_input(\"Enter new product \")\n", "outfile = open(\"prodfile.txt1\",\"a\")\n", "outfile.writelines(cProd)\n", "outfile.close() " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter new product Spoke\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.4, Page No 161" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iProdId = 1\n", "outfile = open(\"prodfile.txt\",\"w\")\n", "while (iProdId != 0):\n", " iProdId = int(raw_input(\"Enter product id: \"))\n", " dPrice = float(raw_input(\"...and price: \"))\n", " if(iProdId > 0):\n", " outfile.writelines(str(iProdId) + '\\n' + str(dPrice) + '\\n')\n", "outfile.close()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product id: 2345\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "...and price: 245.5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product id: 4512\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "...and price: 450.2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product id: 3902\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "...and price: 320.7\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product id: 2478\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "...and price: 75.9\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product id: 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "...and price: 0\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.5, Page No 163" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iFound = 0\n", "infile = open(\"prodfile.txt\",\"r\")\n", "iSrch = int(raw_input(\"Enter product id: \"))\n", "while(1):\n", " iProdId = infile.readline()\n", " if(iProdId == \"\"):\n", " break\n", " iProdId = int(iProdId)\n", " dPrice = float(infile.readline())\n", " if(iProdId == iSrch):\n", " print \"The price is:\" , dPrice\n", " iFound = 1\n", " break\n", "if(not iFound):\n", " print \"Product missing\"\n", "infile.close()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter product id: 1234\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Product missing\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.6, Page No 166" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def sort(cList,n):\n", " for v in range(0,n-1):\n", " for h in range(v+1,n):\n", " if(cList[h]<cList[v]):\n", " temp = cList[v]\n", " cList[v] = cList[h]\n", " cList[h] = temp\n", "i = 0\n", "infile = open(\"prodfile1.txt\",\"r\")\n", "cProd = infile.readlines()\n", "infile.close()\n", "while(i < len(cProd)):\n", " i = i + 1\n", "iNo = i\n", "sort(cProd,iNo)\n", "for j in range(0,iNo):\n", " print cProd[j]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Bike\n", "\n", "Handlebars\n", "\n", "Moped\n", "\n", "Saddle\n", "\n", "Spoke\n", "\n", "Wheel\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.7, Page No 169" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "infile = open(\"prodfile.txt\",\"r\")\n", "outfile = open(\"temp.txt\",\"w\")\n", "iSrch = int(raw_input(\"Specify product id: \"))\n", "while(1):\n", " iProdId = infile.readline()\n", " if(iProdId == \"\"):\n", " break\n", " iProdId = int(iProdId)\n", " dPrice = float(infile.readline())\n", " if(iProdId == iSrch):\n", " dPrice = float(raw_input(\"Specify the new price: \"))\n", " outfile.writelines(str(iProdId) + '\\n' + str(dPrice) + '\\n')\n", "infile.close()\n", "outfile.close()\n", "os.remove(\"prodfile.txt\")\n", "os.rename(\"temp.txt\",\"prodfile.txt\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Specify product id: 2345\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Specify the new price: 33.33\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7.8, Page No 171" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "cNewName = raw_input(\"Specify new file name: \")\n", "infile = open(\"prodfile.txt\",\"r\")\n", "outfile = open(cNewName,\"w\")\n", "if(not os.path.exists(cNewName)):\n", " print \"The file could not be created\"\n", "outfile.writelines(infile.readlines())\n", "infile.close()\n", "outfile.close()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Specify new file name: tryal.txt\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }