{ "metadata": { "name": "", "signature": "sha256:a886cf212382fa732d2b3df088bbcba9a65ba4a31cbe899c80e912ac04c2747f" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "CHAPTER 12:FILE INPUT/OUTPUT" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:391" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "fp=open('PR1.txt','r') #open the file for reading\n", "ch=fp.readlines() #ch will store all content of file\n", "print \"%s\" % (' '.join(ch)) #prints content of file\n", "print \"\\n\"\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "site:http://www.bzu.edu.pk/\n", " user:siteuser@localhost\n", " version:5.5.32-0ubuntu0.12.04.1\n", " db: bzu\n", " tables:\n", " username:web,webadmin,webadministrator23\n", " pass:71dc9f7af599450b23a3b5d54bc665c7,\t49fa1a081c8d5c8dcfa05d730803251a,\t*E8665C4049F515D836A3C8704D0543C6DA0FE96D\n", "\n", "\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:394" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "try:\n", " fp=open('yoyo.txt','r')\n", " for line in fin:\n", " print line\n", " fp.close()\n", "except:\n", " print 'cannot open file'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "cannot open file\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:395" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "nol=0\n", "nom=0\n", "nob=0\n", "noc=0\n", "fp=open('H4CK3R.txt','r')\n", "while True:\n", " ch=fp.read(1)\n", " if not ch:\n", " break\n", " noc+=1\n", " if ch==' ':\n", " nob+=1\n", " if ch=='\\n':\n", " nol+=1\n", " if ch=='\\t':\n", " nom+=1\n", "fp.close()\n", "print \"\\n\"\n", "fp.close()\n", "print \"Number of characters=%d\\n\" % (noc)\n", "print \"Number of blanks=%d\\n\" % (nob)\n", "print \"Number of tabs=%d\\n\" % (nom)\n", "print \"Number of lines=%d\\n\" % (nol)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Number of characters=162\n", "\n", "Number of blanks=21\n", "\n", "Number of tabs=3\n", "\n", "Number of lines=4\n", "\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:396-397" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "import sys\n", "try:\n", " fs=open('H4CK3R.txt','r')\n", "except:\n", " print \"Cannot open file\"\n", " sys.exit(1)\n", "try:\n", " ft=open('python.txt','w')\n", "except:\n", " print \"Cannot open file\"\n", " fs.close()\n", " sys.exit(2)\n", "ch=fs.readlines()\n", "ft.writelines(ch)\n", "fs.close()\n", "ft.close()" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SystemExit", "evalue": "1", "output_type": "pyerr", "traceback": [ "An exception has occurred, use %tb to see the full traceback.\n", "\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Cannot open file\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "To exit: use 'exit', 'quit', or Ctrl-D.\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:399" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "try:\n", " fp=open('POEM.txt','w')\n", "except:\n", " print 'cannot open file'\n", "print \"\\nEnter a few lines of text:\\n\"\n", "s=' '\n", "while (len(s)>0):\n", " s=raw_input()\n", " fp.writelines(s)\n", " fp.writelines(\"\\n\")\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter a few lines of text:\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Shining and bright,they are forever,\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "so true about diamonds,\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "more so of memories,\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "especially yours!\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:400" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "try:\n", " fp=open('POEM.txt','r')\n", "except:\n", " print 'cannot open file'\n", "s=fp.read(99)\n", "print \"%s\" % (s)\n", "print \"\\n\"\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Shining and bright,they are forever,\n", "so true about diamonds,\n", "more so of memories,\n", "especially yours!\n", "\n", "\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:401-402" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "class emp():\n", " def __init__(self,**kwds):\n", " self.__dict__.update(kwds)\n", "another='Y'\n", "e=emp()\n", "try:\n", " fp=open('EMPLOYEE.txt','w')\n", "except:\n", " print 'cannot open file'\n", "while another=='Y':\n", " print \"\\nEnter name,age and basic salary:\"\n", " e.name=raw_input()\n", " e.age=eval(raw_input())\n", " e.bs=eval(raw_input())\n", " ch=\"%s %d %f\" % (e.name,e.age,e.bs)\n", " fp.writelines(ch)\n", " fp.writelines(\"\\n\")\n", " print \"Add another record(Y/N)\"\n", " another=raw_input()\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter name,age and basic salary:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Sunil\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "34\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1250.50\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Add another record(Y/N)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter name,age and basic salary:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Sameer\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "21\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1300.50\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Add another record(Y/N)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter name,age and basic salary:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Rahul\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "34\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1400.55\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Add another record(Y/N)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "n\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:403-404" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "class emp():\n", " def __init__(self,**kwds):\n", " self.__dict__.update(kwds)\n", "e=emp()\n", "try:\n", " fp=open('EMPLOYEE.txt','r') # open the file for reading\n", "except:\n", " print 'cannot open file'\n", "for line in fp: # iterate over each line\n", " e.name, e.age, e.bs = line.split() # split it by whitespace\n", " e.age = int(e.age) # convert age from string to int\n", " e.bs = float(e.bs) # convert bs from string to float\n", " print \"%s %d %f\\n\" %(e.name, e.age, e.bs)\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Sunil 34 1250.500000\n", "\n", "Sameer 21 1300.500000\n", "\n", "Rahul 34 1400.550000\n", "\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:404-405" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "import sys #for exit()\n", "try:\n", " fs=open('H4CK3R.txt','rb') # open the file for reading\n", "except:\n", " print 'cannot open file'\n", " sys.exit(1)\n", "try:\n", " ft=open('python.txt','wb') # open the file for writing\n", "except:\n", " print 'cannot open file'\n", " fs.close()\n", " sys.exit(2)\n", "ch=fs.readlines()\n", "ft.writelines(ch)\n", "fs.close()\n", "ft.close()" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SystemExit", "evalue": "1", "output_type": "pyerr", "traceback": [ "An exception has occurred, use %tb to see the full traceback.\n", "\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "cannot open file\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "To exit: use 'exit', 'quit', or Ctrl-D.\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:407-408" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "class emp():\n", " def __init__(self,**kwds):\n", " self.__dict__.update(kwds)\n", "another='Y'\n", "e=emp()\n", "try:\n", " fp=open('EMP.txt','wb') # open the file for reading\n", "except:\n", " print 'cannot open file' \n", "while another=='Y':\n", " print \"\\nEnter name,age and basic salary:\"\n", " e.name=raw_input()\n", " e.age=eval(raw_input())\n", " e.bs=eval(raw_input())\n", " ch=\"%s %d %f\" % (e.name,e.age,e.bs)\n", " fp.writelines(ch)\n", " print \"Add another record (Y/N)\"\n", " another=raw_input()\n", " if another=='Y':\n", " fp.writelines(\"\\n\")\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter name,age and basic salary:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Suresh\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "24\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1250.50\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Add another record (Y/N)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter name,age and basic salary:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Ranjan\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "21\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1300.60\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Add another record (Y/N)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter name,age and basic salary:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Harish\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "28\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1400.70\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Add another record (Y/N)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "N\n" ] } ], "prompt_number": 21 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:409" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "class emp():\n", " def __init__(self,**kwds):\n", " self.__dict__.update(kwds)\n", "e=emp()\n", "try:\n", " fp=open('EMP.txt','rb')\n", "except:\n", " print \"Cannot open file\"\n", "for line in fp:\n", " e.name, e.age, e.bs = line.split() # split it by whitespace\n", " e.age = int(e.age) # convert age from string to int\n", " e.bs = float(e.bs) # convert bs from string to float\n", " print \"%s %d %f\\n\" %(e.name, e.age, e.bs)\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Suresh 24 1250.500000\n", "\n", "Ranjan 21 1300.600000\n", "\n", "Harish 28 1400.700000\n", "\n" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:411-414" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "class emp():\n", " def __init__(self,**kwds):\n", " self.__dict__.update(kwds)\n", "e=emp()\n", "try:\n", " fp=open('EMP.txt','rb+') # open the file for reading\n", "except:\n", " try:\n", " fp=open('EMP.txt','wb+') # open the file for writing\n", " except:\n", " print 'cannot open file'\n", "while 1:\n", " print \"1.Add Records\"\n", " print \"2.List Records\"\n", " print \"3.Modify Records\"\n", " print \"4.Delete Records\"\n", " print \"0.Exit\"\n", " print \"Your choice\"\n", " choice=eval(raw_input())\n", " def add():\n", " import os\n", " fp.seek(0,os.SEEK_END)\n", " another='Y'\n", " while another=='Y':\n", " print \"\\nEnter name,age and basic salary:\"\n", " e.name=raw_input()\n", " e.age=eval(raw_input())\n", " e.bs=eval(raw_input())\n", " ch=\"%s %d %f\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\" % (e.name,e.age,e.bs)\n", " fp.writelines(\"\\n\")\n", " fp.writelines(ch)\n", " print \"Add another record(Y/N)\"\n", " another=raw_input()\n", " def list():\n", " import os\n", " fp.seek(0,os.SEEK_SET)\n", " for line in fp: # iterate over each line\n", " if len(line)>10:\n", " e.name, e.age, e.bs = line.split() # split it by whitespace\n", " e.age = int(e.age) # convert age from string to int\n", " e.bs = float(e.bs) # convert bs from string to float\n", " print \"%s %d %f\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\" %(e.name, e.age, e.bs)\n", " def modify():\n", " another='Y'\n", " while another=='Y':\n", " print \"\\nEnter name of employee to modify\"\n", " empname=raw_input()\n", " import os\n", " fp.seek(0,os.SEEK_SET)\n", " for line in iter(fp.readline, ''):\n", " if len(line)>10:\n", " e.name, e.age, e.bs=line.split()\n", " e.age = int(e.age) # convert age from string to int\n", " e.bs = float(e.bs)\n", " if(cmp(e.name,empname)==0):\n", " c=len(line)\n", " print \"\\nEnter new name,age & bs\"\n", " e.name=raw_input()\n", " e.age=eval(raw_input())\n", " e.bs=eval(raw_input())\n", " import os\n", " fp.seek(-c,os.SEEK_CUR)\n", " ch=\"%s %d %f\" % (e.name,e.age,e.bs)\n", " fp.writelines(\"\\n\")\n", " fp.writelines(ch)\n", " fp.writelines(\"\\n\")\n", " break\n", " print \"\\nModify another Record(Y/N)\"\n", " another=raw_input()\n", " def delete():\n", " another='Y'\n", " global fp\n", " while another=='Y':\n", " print \"\\nEnter name of employee to delete\"\n", " empname=raw_input()\n", " ft=open('TEMP.txt','wb')\n", " import os\n", " fp.seek(0,os.SEEK_SET)\n", " for line in fp:\n", " if len(line)>10:\n", " e.name, e.age, e.bs=line.split()\n", " e.age = int(e.age) # convert age from string to int\n", " e.bs = float(e.bs)\n", " if(cmp(e.name,empname)!=0):\n", " ch=\"%s %d %f\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\" % (e.name,e.age,e.bs)\n", " ft.writelines(ch)\n", " ft.writelines(\"\\n\")\n", " fp.close()\n", " ft.close()\n", " import os\n", " os.remove(\"EMP.txt\") # Delete file EMP.txt\n", " os.rename( \"TEMP.txt\", \"D:/EMP.txt\" ) # Rename a file from TEMP.txt to EMP.txt\n", " fp=open('EMP.txt','rb+')\n", " print \"Delete another record(Y/N)\"\n", " another=raw_input()\n", " def exit():\n", " import sys\n", " fp.close()\n", " sys.exit(0)\n", " def switch(c):\n", " return {1: add,\n", " 2: list,\n", " 3: modify,\n", " 4: delete,\n", " 0: exit,\n", " }[c]()\n", " switch(choice)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1.Add Records\n", "2.List Records\n", "3.Modify Records\n", "4.Delete Records\n", "0.Exit\n", "Your choice\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n", "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n", "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n", "1.Add Records\n", "2.List Records\n", "3.Modify Records\n", "4.Delete Records\n", "0.Exit\n", "Your choice\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter name,age and basic salary:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Ram\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "20\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5000\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Add another record(Y/N)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter name,age and basic salary:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "GOPAL\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "19\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6755.5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Add another record(Y/N)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "N\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1.Add Records\n", "2.List Records\n", "3.Modify Records\n", "4.Delete Records\n", "0.Exit\n", "Your choice\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n", "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n", "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n", "Ram 20 5000.000000\t\t\t\t\t\t\t\t\t\t\n", "GOPAL 19 6755.500000\t\t\t\t\t\t\t\t\t\t\n", "1.Add Records\n", "2.List Records\n", "3.Modify Records\n", "4.Delete Records\n", "0.Exit\n", "Your choice\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter name of employee to modify\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Ram\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter new name,age & bs\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Radhey\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "15\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4687.66\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Modify another Record(Y/N)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "N\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1.Add Records\n", "2.List Records\n", "3.Modify Records\n", "4.Delete Records\n", "0.Exit\n", "Your choice\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n", "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n", "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n", "Radhey 15 4687.660000\t\t\t\t\t\t\t\t\t\t\n", "GOPAL 19 6755.500000\t\t\t\t\t\t\t\t\t\t\n", "1.Add Records\n", "2.List Records\n", "3.Modify Records\n", "4.Delete Records\n", "0.Exit\n", "Your choice\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter name of employee to delete\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Radhey\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Delete another record(Y/N)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "N\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1.Add Records\n", "2.List Records\n", "3.Modify Records\n", "4.Delete Records\n", "0.Exit\n", "Your choice\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n", "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n", "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n", "GOPAL 19 6755.500000\t\t\t\t\t\t\t\t\t\t\n", "1.Add Records\n", "2.List Records\n", "3.Modify Records\n", "4.Delete Records\n", "0.Exit\n", "Your choice\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter name of employee to delete\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "GOPAL\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Delete another record(Y/N)\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "N\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "1.Add Records\n", "2.List Records\n", "3.Modify Records\n", "4.Delete Records\n", "0.Exit\n", "Your choice\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n", "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n", "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n", "1.Add Records\n", "2.List Records\n", "3.Modify Records\n", "4.Delete Records\n", "0.Exit\n", "Your choice\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "ename": "SystemExit", "evalue": "0", "output_type": "pyerr", "traceback": [ "An exception has occurred, use %tb to see the full traceback.\n", "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 0\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "To exit: use 'exit', 'quit', or Ctrl-D.\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "EXAMPLE ON PAGE:416-417" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "from StringIO import StringIO\n", "buffer = StringIO()\n", "print \"\\nEnter source file name\"\n", "source=raw_input()\n", "try:\n", " inhandle=open(source,'rb')\n", "except:\n", " print \"Cannot open file\"\n", "print \"Enter target file name\"\n", "target=raw_input()\n", "try:\n", " outhandle=open(target,'wb')\n", "except:\n", " print \"Cannot open file\"\n", " inhandle.close()\n", "bytes=inhandle.readlines()\n", "buffer.write(bytes)\n", "outhandle.writelines(buffer.getvalue())\n", "buffer.close()\n", "inhandle.close()\n", "outhandle.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter source file name\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "H4CK3R.txt\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter target file name\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "python.txt\n" ] } ], "prompt_number": 24 } ], "metadata": {} } ] }