{ "metadata": { "name": "chapter-12.ipynb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 12: File Input/Output

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Display Contents of a File , Page number: 417

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "fp = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #open file in read mode\n", "while ( 1 ):\n", " ch = fp.read(1)\n", " if not ch :\n", " break\n", " print ch\n", "\n", "fp.close() #close file\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "M\n", "e\n", "t\n", "h\n", "o\n", "d\n", "\n", "\n", "T\n", "h\n", "r\n", "o\n", "u\n", "g\n", "h\n", "l\n", "y\n", " \n", "w\n", "a\n", "s\n", " \n", "b\n", "o\n", "t\n", "h\n", " \n", "b\n", "e\n", "e\n", "t\n", "r\n", "o\n", "o\n", "t\n", " \n", "a\n", "n\n", "d\n", " \n", "c\n", "a\n", "r\n", "r\n", "o\n", "t\n", " \n", "t\n", "o\n", " \n", "r\n", "e\n", "m\n", "o\n", "v\n", "e\n", " \n", "t\n", "h\n", "e\n", " \n", "m\n", "u\n", "d\n", " \n", "o\n", "n\n", " \n", "b\n", "e\n", "e\n", "t\n", "r\n", "o\n", "o\n", "t\n", " \n", "a\n", "n\n", "d\n", " \n", "t\n", "h\n", "e\n", " \n", "h\n", "a\n", "i\n", "r\n", " \n", "o\n", "n\n", " \n", "c\n", "a\n", "r\n", "r\n", "o\n", "t\n", "\n", "\n", "P\n", "e\n", "e\n", "l\n", " \n", "o\n", "f\n", "f\n", " \n", "t\n", "h\n", "e\n", " \n", "c\n", "a\n", "r\n", "r\n", "o\n", "t\n", " \n", "s\n", "k\n", "i\n", "n\n", ".\n", " \n", "d\n", "o\n", " \n", "n\n", "o\n", "t\n", " \n", "p\n", "e\n", "e\n", "l\n", " \n", "t\n", "h\n", "e\n", " \n", "r\n", "a\n", "d\n", "d\n", "i\n", "s\n", "h\n", " \n", "s\n", "k\n", "i\n", "n\n", " \n", "t\n", "o\n", " \n", "a\n", "v\n", "o\n", "i\n", "d\n", " \n", "p\n", "o\n", "s\n", "s\n", "i\n", "b\n", "l\n", "e\n", " \n", "l\n", "o\n", "s\n", "s\n", " \n", "o\n", "f\n", " \n", "v\n", "i\n", "t\n", "a\n", "m\n", "i\n", "n\n", "s\n", "\n", "\n", "C\n", "u\n", "t\n", " \n", "e\n", "a\n", "c\n", "h\n", " \n", "b\n", "e\n", "e\n", "t\n", "r\n", "o\n", "o\n", "t\n", " \n", "a\n", "n\n", "d\n", " \n", "c\n", "a\n", "r\n", "r\n", "o\n", "t\n", " \n", "i\n", "n\n", " \n", "t\n", "o\n", " \n", "4\n", " \n", "h\n", "a\n", "l\n", "v\n", "e\n", "s\n", "\n", "\n", "P\n", "r\n", "e\n", "a\n", "s\n", "s\n", "u\n", "r\n", "e\n", " \n", "c\n", "o\n", "o\n", "k\n", " \n", "t\n", "h\n", "e\n", " \n", "b\n", "e\n", "e\n", "t\n", "r\n", "o\n", "o\n", "t\n", " \n", "a\n", "n\n", "d\n", " \n", "c\n", "a\n", "r\n", "r\n", "o\n", "t\n", ".\n", " \n", "m\n", "a\n", "k\n", "e\n", " \n", "3\n", " \n", "w\n", "h\n", "i\n", "s\n", "t\n", "l\n", "e\n", "s\n", " \n", "s\n", "o\n", " \n", "t\n", "h\n", "e\n", "t\n", " \n", "t\n", "h\n", "e\n", "y\n", " \n", "b\n", "e\n", "c\n", "o\n", "m\n", "e\n", " \n", "v\n", "e\n", "r\n", "y\n", " \n", "s\n", "m\n", "o\n", "o\n", "t\n", "h\n", "\n", "\n", "D\n", "r\n", "a\n", "i\n", "n\n", " \n", "a\n", "n\n", "d\n", " \n", "r\n", "e\n", "s\n", "e\n", "r\n", "v\n", "e\n", " \n", "t\n", "h\n", "e\n", " \n", "w\n", "a\n", "t\n", "e\n", "r\n", " \n", "a\n", "f\n", "t\n", "e\n", "r\n", " \n", "p\n", "r\n", "e\n", "s\n", "s\n", "u\n", "r\n", "e\n", " \n", "c\n", "o\n", "o\n", "k\n", "i\n", "n\n", "g\n", ".\n", "\n", "\n", "M\n", "a\n", "s\n", "h\n", " \n", "t\n", "h\n", "e\n", " \n", "c\n", "o\n", "o\n", "k\n", "e\n", "d\n", " \n", "p\n", "i\n", "e\n", "c\n", "e\n", "s\n", " \n", "a\n", "n\n", "d\n", " \n", "a\n", "l\n", "l\n", "o\n", "w\n", " \n", "t\n", "o\n", " \n", "c\n", "o\n", "o\n", "l\n", "\n", "\n", "C\n", "u\n", "t\n", " \n", "o\n", "n\n", "i\n", "o\n", "n\n", "s\n", " \n", "i\n", "n\n", "t\n", "o\n", " \n", "c\n", "u\n", "b\n", "e\n", "s\n", " \n", "a\n", "n\n", "d\n", " \n", "s\n", "a\n", "u\n", "t\n", "e\n", " \n", "t\n", "i\n", "l\n", "l\n", " \n", "g\n", "o\n", "l\n", "d\n", "e\n", "n\n", " \n", "b\n", "r\n", "o\n", "w\n", "n\n", " \n", "o\n", "n\n", " \n", "o\n", "i\n", "l\n", " \n", "a\n", "n\n", "d\n", " \n", "a\n", "l\n", "l\n", "o\n", "w\n", " \n", "t\n", "o\n", " \n", "c\n", "o\n", "o\n", "l\n", "\n", "\n", "A\n", "f\n", "t\n", "e\n", "r\n", " \n", "c\n", "o\n", "o\n", "l\n", "i\n", "n\n", "g\n", " \n", "g\n", "r\n", "i\n", "n\n", "d\n", " \n", "t\n", "h\n", "e\n", " \n", "m\n", "a\n", "s\n", "h\n", "e\n", "d\n", " \n", "b\n", "e\n", "e\n", "t\n", "r\n", "o\n", "o\n", "t\n", ",\n", " \n", "c\n", "a\n", "r\n", "r\n", "o\n", "t\n", " \n", "a\n", "n\n", "d\n", " \n", "s\n", "a\n", "u\n", "t\n", "e\n", "d\n", " \n", "o\n", "n\n", "i\n", "o\n", "n\n", "s\n", " \n", "t\n", "o\n", " \n", "p\n", "a\n", "s\n", "t\n", "e\n", " \n", "i\n", "n\n", " \n", "a\n", " \n", "m\n", "i\n", "x\n", "y\n", ".\n", " \n", "a\n", "d\n", "d\n", " \n", "e\n", "n\n", "o\n", "u\n", "g\n", "h\n", " \n", "w\n", "a\n", "t\n", "e\n", "r\n", " \n", "t\n", "o\n", " \n", "m\n", "a\n", "k\n", "e\n", " \n", "p\n", "a\n", "s\n", "t\n", "e\n", " \n", "n\n", "o\n", "t\n", " \n", "v\n", "e\n", "r\n", "y\n", " \n", "f\n", "i\n", "n\n", "e\n", ".\n", "\n", "\n", "N\n", "o\n", "w\n", " \n", "s\n", "t\n", "r\n", "a\n", "i\n", "n\n", " \n", "t\n", "h\n", "e\n", " \n", "g\n", "r\n", "i\n", "n\n", "d\n", " \n", "p\n", "a\n", "s\n", "t\n", "e\n", ".\n", "\n", "\n", "T\n", "a\n", "k\n", "e\n", " \n", "s\n", "o\n", "m\n", "e\n", " \n", "b\n", "u\n", "t\n", "t\n", "e\n", "r\n", " \n", "i\n", "n\n", " \n", "a\n", " \n", "v\n", "e\n", "s\n", "s\n", "e\n", "l\n", " \n", "a\n", "d\n", "d\n", " \n", "t\n", "h\n", "e\n", " \n", "g\n", "a\n", "r\n", "l\n", "i\n", "c\n", " \n", "p\n", "a\n", "s\n", "t\n", "e\n", ",\n", "g\n", "i\n", "n\n", "g\n", "e\n", "r\n", "p\n", "a\n", "s\n", "t\n", "e\n", " \n", "a\n", "n\n", "d\n", " \n", "g\n", "r\n", "e\n", "e\n", "n\n", " \n", "c\n", "h\n", "i\n", "l\n", "l\n", "i\n", " \n", "p\n", "a\n", "s\n", "t\n", "e\n", " \n", "a\n", "n\n", "d\n", " \n", "s\n", "a\n", "u\n", "t\n", "e\n", " \n", "f\n", "o\n", "r\n", " \n", "s\n", "o\n", "m\n", "e\n", " \n", "t\n", "i\n", "m\n", "e\n", " \n", "t\n", "i\n", "l\n", "l\n", " \n", "u\n", " \n", "g\n", "e\n", "t\n", " \n", "t\n", "h\n", "e\n", " \n", "s\n", "p\n", "e\n", "c\n", "i\n", "f\n", "i\n", "c\n", " \n", "g\n", "a\n", "r\n", "l\n", "i\n", "c\n", "-\n", "b\n", "e\n", "i\n", "n\n", "g\n", "-\n", "f\n", "r\n", "i\n", "e\n", "d\n", " \n", "s\n", "m\n", "e\n", "l\n", "l\n", "\n", "\n", "A\n", "d\n", "d\n", " \n", "m\n", "o\n", "o\n", "n\n", "g\n", " \n", "s\n", "p\n", "r\n", "o\n", "u\n", "t\n", "s\n", " \n", "a\n", "n\n", "d\n", " \n", "s\n", "a\n", "u\n", "t\n", "e\n", ",\n", " \n", "c\n", "o\n", "v\n", "e\n", "r\n", " \n", "w\n", "i\n", "t\n", "h\n", " \n", "t\n", "h\n", "e\n", " \n", "l\n", "i\n", "d\n", " \n", "a\n", "n\n", "d\n", " \n", "l\n", "e\n", "t\n", " \n", "c\n", "o\n", "o\n", "k\n", " \n", "f\n", "o\n", "r\n", " \n", "5\n", " \n", "m\n", "i\n", "n\n", "s\n", "\n", "\n", "N\n", "o\n", "w\n", " \n", "a\n", "d\n", "d\n", " \n", "t\n", "h\n", "e\n", " \n", "s\n", "t\n", "r\n", "a\n", "i\n", "n\n", " \n", "o\n", "f\n", " \n", "b\n", "e\n", "e\n", "t\n", "r\n", "o\n", "o\n", "t\n", ",\n", "c\n", "a\n", "r\n", "r\n", "o\n", "t\n", " \n", "a\n", "n\n", "d\n", " \n", "o\n", "n\n", "i\n", "o\n", "n\n", "s\n", " \n", "p\n", "a\n", "s\n", "t\n", "e\n", "\n", "\n", "A\n", "d\n", "d\n", " \n", "6\n", " \n", "c\n", "u\n", "p\n", "s\n", " \n", "o\n", "f\n", " \n", "w\n", "a\n", "t\n", "e\n", "r\n", " \n", "t\n", "o\n", " \n", "i\n", "t\n", " \n", "a\n", "n\n", "d\n", " \n", "a\n", "l\n", "s\n", "o\n", " \n", "t\n", "h\n", "e\n", " \n", "w\n", "a\n", "t\n", "e\n", "r\n", " \n", "r\n", "e\n", "s\n", "e\n", "r\n", "v\n", "e\n", "d\n", " \n", "a\n", "t\n", " \n", "t\n", "h\n", "e\n", " \n", "t\n", "i\n", "m\n", "e\n", " \n", "o\n", "f\n", " \n", "p\n", "r\n", "e\n", "s\n", "s\n", "u\n", "r\n", "e\n", " \n", "c\n", "o\n", "o\n", "k\n", "i\n", "n\n", "g\n", " \n", "t\n", "h\n", "e\n", " \n", "b\n", "e\n", "e\n", "t\n", "r\n", "o\n", "o\n", "t\n", " \n", "a\n", "n\n", "d\n", " \n", "c\n", "a\n", "r\n", "r\n", "o\n", "t\n", ".\n", "\n", "\n", "A\n", "d\n", "d\n", " \n", "s\n", "a\n", "l\n", "t\n", ",\n", " \n", "c\n", "i\n", "n\n", "n\n", "a\n", "m\n", "o\n", "n\n", ",\n", " \n", "c\n", "u\n", "m\n", "m\n", "i\n", "n\n", ",\n", " \n", "c\n", "o\n", "r\n", "i\n", "a\n", "n\n", "d\n", "e\n", "r\n", " \n", "p\n", "o\n", "w\n", "d\n", "e\n", "r\n", ",\n", "l\n", "e\n", "m\n", "o\n", "n\n", " \n", "j\n", "u\n", "i\n", "c\n", "e\n", "\n", "\n", "B\n", "r\n", "i\n", "n\n", "g\n", " \n", "t\n", "o\n", " \n", "a\n", " \n", "b\n", "o\n", "i\n", "l\n", ",\n", " \n", "l\n", "e\n", "t\n", " \n", "s\n", "i\n", "m\n", "m\n", "e\n", "r\n", " \n", "f\n", "o\n", "r\n", " \n", "a\n", " \n", "f\n", "e\n", "w\n", " \n", "m\n", "i\n", "n\n", "s\n", "\n", "\n", "M\n", "a\n", "k\n", "e\n", " \n", "a\n", " \n", "f\n", "i\n", "n\n", "e\n", " \n", "p\n", "a\n", "s\n", "t\n", "e\n", " \n", "o\n", "f\n", " \n", "c\n", "o\n", "r\n", "n\n", "f\n", "l\n", "o\n", "u\n", "r\n", " \n", "a\n", "n\n", "d\n", " \n", "a\n", "d\n", "d\n", " \n", "i\n", "t\n", " \n", "w\n", "h\n", "i\n", "l\n", "e\n", " \n", "s\n", "t\n", "i\n", "r\n", "r\n", "i\n", "n\n", "g\n", " \n", "t\n", "o\n", " \n", "a\n", "v\n", "o\n", "i\n", "d\n", " \n", "l\n", "u\n", "m\n", "p\n", "s\n", "\n", "\n", "T\n", "i\n", "l\n", " \n", "f\n", "o\n", "r\n", " \n", "s\n", "o\n", "m\n", "e\n", "m\n", "o\n", "r\n", "e\n", " \n", "t\n", "i\n", "m\n", "e\n", " \n", "s\n", "o\n", " \n", "t\n", "h\n", "a\n", "t\n", " \n", "t\n", "h\n", "e\n", " \n", "c\n", "o\n", "r\n", "n\n", "f\n", "l\n", "o\n", "u\n", "r\n", " \n", "p\n", "a\n", "s\n", "t\n", "e\n", " \n", "i\n", "s\n", " \n", "c\n", "o\n", "o\n", "k\n", "e\n", "d\n", " \n", "a\n", "n\n", "d\n", " \n", "m\n", "i\n", "x\n", "e\n", "d\n", " \n", "w\n", "e\n", "l\n", "l\n", "\n", "\n", "T\n", "a\n", "k\n", "e\n", " \n", "o\n", "i\n", "l\n", " \n", "i\n", "n\n", " \n", "a\n", " \n", "k\n", "a\n", "d\n", "h\n", "a\n", "i\n", " \n", "t\n", "o\n", " \n", "f\n", "r\n", "y\n", " \n", "t\n", "h\n", "e\n", " \n", "n\n", "o\n", "o\n", "d\n", "l\n", "e\n", "s\n", ".\n", "f\n", "r\n", "y\n", " \n", "t\n", "i" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "l\n", "l\n", " \n", "g\n", "o\n", "l\n", "d\n", "e\n", "n\n", " \n", "b\n", "r\n", "o\n", "w\n", "n\n", " \n", "a\n", "n\n", "d\n", " \n", "r\n", "e\n", "s\n", "e\n", "r\n", "v\n", "e\n", " \n", "o\n", "n\n", " \n", "a\n", " \n", "t\n", "i\n", "s\n", "s\n", "u\n", "e\n", " \n", "p\n", "a\n", "p\n", "e\n", "r\n", "\n", "\n", "C\n", "r\n", "u\n", "s\n", "h\n", " \n", "t\n", "h\n", "e\n", " \n", "n\n", "o\n", "o\n", "d\n", "l\n", "e\n", "s\n", " \n", "n\n", "o\n", "t\n", " \n", "t\n", "o\n", "o\n", " \n", "m\n", "u\n", "c\n", "h\n", "\n", "\n", "S\n", "e\n", "r\n", "v\n", "e\n", " \n", "s\n", "t\n", "e\n", "a\n", "m\n", "i\n", "n\n", "g\n", " \n", "h\n", "o\n", "t\n", " \n", "i\n", "n\n", " \n", "s\n", "o\n", "u\n", "p\n", " \n", "b\n", "o\n", "w\n", "l\n", "s\n", " \n", "a\n", "d\n", "d\n", " \n", "s\n", "o\n", "m\n", "e\n", " \n", "b\n", "u\n", "t\n", "t\n", "e\n", "r\n", " \n", "a\n", "n\n", "d\n", " \n", "t\n", "o\n", "p\n", " \n", "w\n", "i\n", "t\n", "h\n", " \n", "t\n", "h\n", "e\n", " \n", "c\n", "r\n", "i\n", "s\n", "p\n", "y\n", " \n", "n\n", "o\n", "o\n", "d\n", "l\n", "e\n", "s\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Opening a File , Page number: 421

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "try :\n", " fp = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" )#open file in read mode and check if file is opened successfully\n", "except:\n", " print \"cannot open file\" \n", " exit()\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "cannot open file\n" ] } ], "prompt_number": "*" }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Counting Characters,Tabs,Spaces and Newlines , Page number: 423

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "nol = 0\n", "Not = 0\n", "nob = 0\n", "noc = 0\n", "\n", "fp = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #open file in read mode\n", "while ( 1 ):\n", " ch = fp.read(1)\n", " if not ch :\n", " break\n", " noc = noc + 1 #number of chars\n", " if ( ch == \" \" ):\n", " nob = nob + 1 #number of spaces\n", " if ( ch == \"\\n\" ):\n", " nol = nol + 1 # number of newlines\n", " if ( ch == \"\\t\" ):\n", " Not = Not + 1 #number of tabs\n", "\n", "fp.close() #close file\n", "\n", "#Result\n", "print \"Number of characters = \", noc \n", "print \"Number of blanks = \", nob \n", "print \"Number of tabs = \", Not \n", "print \"Number of lines = \", nol \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Number of characters = 1495\n", "Number of blanks = 254\n", "Number of tabs = 0\n", "Number of lines = 20\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

File Copy , Page number: 424

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "fs = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #open file in read mode\n", "if(not fs):\n", " print \"Cannot open source file\"\n", " exit()\n", "\n", "ft = open ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"w\" ) #open file in write mode\n", "if(not ft):\n", " print \"Cannot open target file\"\n", " fs.close() \n", " exit()\n", " \n", "while ( 1 ):\n", " ch = fs.read(1)\n", " if not ch :\n", " break\n", " else:\n", " ft.writelines(ch) #write into target file\n", "\n", "#closen files\n", "fs.close()\n", "ft.close()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Writing Strings to Files, Page number: 427

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "fp = open ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"w\" ) #open file in write mode\n", "if(not fp):\n", " print \"Cannot open target file\" \n", " exit()\n", "\n", "print \"Enter a few lines of text:\" \n", "#s=input(\"\") #Input strings from keyboard\n", "s = \"File written\"\n", "print s\n", "while ( len(s) > 0 ):\n", " fp.writelines(s) #write into file\n", " fp.writelines(\"\\n\")\n", " #s=input(\"\") #Input strings from keyboard\n", " s=\"\"\n", " \n", "#close files\n", "fp.close()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a few lines of text:\n", "File written\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Reading Strings from Files , Page number: 429

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "fp = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #open file in read mode\n", "if ( not fp ):\n", " print \"Cannot open file\" \n", " exit( ) \n", "\n", "while ( 1 ) :#Read strings from file\n", " s = fp.read(79)\n", " if(s):\n", " print s\n", " else:\n", " break\n", " \n", "\n", "fp.close() #close file\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Method\n", "Throughly was both beetroot and carrot to remove the mud on beetroot and\n", " the hair on carrot\n", "Peel off the carrot skin. do not peel the raddish skin to a\n", "void possible loss of vitamins\n", "Cut each beetroot and carrot in to 4 halves\n", "Prea\n", "ssure cook the beetroot and carrot. make 3 whistles so thet they become very sm\n", "ooth\n", "Drain and reserve the water after pressure cooking.\n", "Mash the cooked pieces\n", " and allow to cool\n", "Cut onions into cubes and saute till golden brown on oil and\n", " allow to cool\n", "After cooling grind the mashed beetroot, carrot and sauted onion\n", "s to paste in a mixy. add enough water to make paste not very fine.\n", "Now strain \n", "the grind paste.\n", "Take some butter in a vessel add the garlic paste,gingerpaste \n", "and green chilli paste and saute for some time till u get the specific garlic-b\n", "eing-fried smell\n", "Add moong sprouts and saute, cover with the lid and let cook f\n", "or 5 mins\n", "Now add the strain of beetroot,carrot and onions paste\n", "Add 6 cups of \n", "water to it and also the water reserved at the time of pressure cooking the bee\n", "troot and carrot.\n", "Add salt, cinnamon, cummin, coriander powder,lemon juice\n", "Brin\n", "g to a boil, let simmer for a few mins\n", "Make a fine paste of cornflour and add i\n", "t while stirring to avoid lumps\n", "Til for somemore time so that the cornflour pas\n", "te is cooked and mixed well\n", "Take oil in a kadhai to fry the noodles.fry till go\n", "lden brown and reserve on a tissue paper\n", "Crush the noodles not too much\n", "Serve s\n", "teaming hot in soup bowls add some butter and top with the crispy noodles\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Writing Records to Files , Page number: 431

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from collections import namedtuple\n", "\n", "#Variable declaration\n", "another = 'Y'\n", "\n", "#Structure defintion\n", "struct_emp = namedtuple(\"struct_emp\", \"name age bs\")\n", "\n", "\n", "fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.dat\", \"w\" ) #open file in write mode\n", "if(not fp):\n", " print \"Cannot open target file\"\n", " exit()\n", "\n", "while ( another == 'Y' ):\n", " print \"Enter name, age and basic salary: \" \n", " #en,ea,ebs = input(\"\").split()\n", " en =\"John\"\n", " ea=\"34\"\n", " ebs=\"25000\"\n", " print en,ea,ebs\n", " e = struct_emp(en,ea,ebs)\n", " fp.writelines(e) #write into file\n", " fp.writelines(\"\\n\")\n", " #another = input( \"Add another record (Y/N): \" ) \n", " print \"Add another record (Y/N): \"\n", " another = 'N'\n", " print another\n", "#close file\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter name, age and basic salary: \n", "John 34 25000\n", "Add another record (Y/N): \n", "N\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Reading Records from File , Page number: 433

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from collections import namedtuple\n", "#Structure defintion\n", "struct_emp = namedtuple(\"struct_emp\", \"name age bs\")\n", "\n", "\n", "fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.txt\", \"r\" ) #open file in read mode\n", "if(not fp):\n", " print \"Cannot open target file\"\n", " exit()\n", "\n", "while ( 1 ):\n", " s = fp.readline()\n", " if(s):\n", " en,ea,ebs = s.split()\n", " e = struct_emp(en,ea,ebs) #Read record\n", " print e.name, e.age, e.bs \n", " else:\n", " break\n", " \n", "#close file\n", "fp.close()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "John 34 25000\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Copying Binary Files , Page number: 434

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "fs = open (\"C:/Users/Akshatha M/Desktop/Project1.exe\", \"rb\" ) #open file in read binary mode\n", "if(not fs):\n", " print \"Cannot open source file\"\n", " exit()\n", "\n", "ft = open ( \"C:/Users/Akshatha M/Desktop/NewProject1.exe\", \"wb\" ) #open file in write binary mode\n", "if(not ft):\n", " print \"Cannot open target file\"\n", " fs.close() \n", " exit()\n", " \n", "while ( 1 ):\n", " ch = fs.read(1)\n", " if not ch :\n", " break\n", " else:\n", " ft.write(ch) #write into target file\n", "\n", "#closen files\n", "fs.close()\n", "ft.close()\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Writing To Files in Binary Mode, Page number: 438

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from collections import namedtuple\n", "\n", "#Variable declaration\n", "another = 'Y'\n", "\n", "#Structure defintion\n", "struct_emp = namedtuple(\"struct_emp\", \"name age bs\")\n", "\n", "fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.dat\", \"wb\" ) #open file in write mode\n", "if(not fp):\n", " print \"Cannot open target file\" \n", " exit()\n", "\n", "while ( another == 'Y' ):\n", " print ( \"Enter name, age and basic salary: \" )\n", " #en,ea,ebs = input(\"\").split()\n", " en =\"John\"\n", " ea=\"34\"\n", " ebs=\"25000\"\n", " print en,ea,ebs\n", " e = struct_emp(en,ea,ebs)\n", " #write into file\n", " fp.write(b'e.name')\n", " fp.write(b'e.age')\n", " fp.write(b'e.bs')\n", " fp.write(b'\\n')\n", " #another = input( \"Add another record (Y/N): \" ) \n", " print \"Add another record (Y/N): \"\n", " another = 'N'\n", " print another\n", "\n", "#close file\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter name, age and basic salary: \n", "John 34 25000\n", "Add another record (Y/N): \n", "N\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Reading From Binary Files , Page number: 440

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "from collections import namedtuple\n", "#Structure defintion\n", "struct_emp = namedtuple(\"struct_emp\", \"name age bs\")\n", "\n", "\n", "fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.txt\", \"rb\" ) #open file in read mode\n", "if(not fp):\n", " print \"Cannot open target file\" \n", " exit()\n", "\n", "while ( 1 ):\n", " s = fp.readline()\n", " if(s):\n", " en,ea,ebs = s.split()\n", " e = struct_emp(en,ea,ebs) #Read record\n", " print e.name, e.age, e.bs \n", " else:\n", " break\n", " \n", "#close file\n", "fp.close()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "John 34 25000\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Database Management, Page number: 442

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import os\n", "from collections import namedtuple\n", "#Structure defintion\n", "struct_emp = namedtuple(\"struct_emp\", \"name age bs\")\n", "\n", "fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.txt\", \"r+b\")\n", "if ( not fp ):\n", " fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.txt\", \"w+b\" )\n", " if ( not fp ):\n", " print \"Cannot open file\" \n", " exit( )\n", "\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", " #choice = input(\"\\nYour choice\")\n", " print \"\\nYour choice\"\n", " choice = 1\n", " if(choice == '1'):\n", " another = 'Y'\n", " while ( another == 'Y' ):\n", " print ( \"Enter name, age and basic salary: \" )\n", " en,ea,ebs = input(\"\").split()\n", " e = struct_emp(en,ea,ebs)\n", " #write into file\n", " fp.write(b'e.name')\n", " fp.write(b' ')\n", " fp.write(b'e.age')\n", " fp.write(b' ')\n", " fp.write(b'e.bs')\n", " fp.write(b'\\n')\n", " another = input( \"Add another record (Y/N): \" )\n", " elif(choice == '2'):\n", " fp.seek(0,0)\n", " while ( 1 ):\n", " s = fp.readline()\n", " if(s):\n", " en,j1,ea,j2,ebs = s.split()\n", " e = struct_emp(en,ea,ebs) #Read record\n", " print ( e.name, e.age, e.bs )\n", " else:\n", " break\n", " elif(choice == '3'):\n", " another = 'Y'\n", " while ( another == 'Y' ):\n", " empname =input(\"Enter name of employee to modify \" )\n", " fp.seek(0,0)\n", " while (fp.readline()):\n", " if ( b'empname' == b'e.name' ):\n", " en,ea,ebs = input(\"Enter new name, age & bs\" ).spilt()\n", " e = struct_emp(en,ea,ebs)\n", " cur = fp.tell()\n", " fp.write(b'e.name')\n", " fp.write(b'e.age')\n", " fp.write(b'e.bs')\n", " fp.write(b'\\n')\n", " break\n", " print ( \"\\nModify another Record (Y/N) \" )\n", " another = input(\"\")\n", " elif(choice == '4'):\n", " another = 'Y'\n", " while ( another == 'Y' ):\n", " empname = input(\"Enter name of employee to delete \" )\n", " ft = open ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"wb\" )\n", " fp.seek(0,0)\n", " while ( 1 ):\n", " s = fp.readline()\n", " if(s):\n", " if ( not(b'empname' == b'e.name' )):\n", " ft.write(s)\n", " else:\n", " break\n", " fp.close()\n", " ft.close()\n", " os.remove(\"C:/Users/Akshatha M/Desktop/Employee.txt\")\n", " os.rename ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"C:/Users/Akshatha M/Desktop/Employee.txt\" )\n", " fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.txt\", \"r+b\" )\n", " print ( \"Delete another Record (Y/N) \" )\n", " another = input(\"\")\n", "\n", " else:\n", " fp.close()#close file\n", " exit( )\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Low Level File Copy, Page number: 448

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#source=input( \"Enter source file name \" ) \n", "print \"Enter source file name \" \n", "source = \"Employee.txt\"\n", "print source\n", "source=\"C:/Users/Akshatha M/Desktop/\"+source\n", "\n", "inhandle = open (source, \"rb\" ) #open file in read binary mode\n", "if(not inhandle):\n", " print \"Cannot open file\"\n", " exit()\n", "\n", "#target=input( \"Enter target file name \" ) \n", "print \"Enter target file name \"\n", "target = \"temp.txt\"\n", "print target\n", "target=\"C:/Users/Akshatha M/Desktop/\"+target\n", "\n", "outhandle = open ( target, \"wb\" ) #open file in write binary mode\n", "if(not outhandle):\n", " print \"Cannot open target file\"\n", " inhandle.close() \n", " exit()\n", "\n", "\n", " \n", "while ( 1 ):\n", " Bytes = inhandle.read(1)\n", " if not Bytes :\n", " break\n", " else:\n", " outhandle.write(Bytes) #write into target file\n", "\n", "#closen files\n", "inhandle.close()\n", "outhandle.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter source file name \n", "Employee.txt\n", "Enter target file name \n", "temp.txt\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Detecting Errors in Reading/Writing, Page number: 470

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "fp = open ( \"C:/Users/Akshatha/Documents/extrastuff/skills.txt\", \"w\" ) \n", "while (1):\n", " try:\n", " ch = fp.read(1) #read character from file \n", " except: #ferror()\n", " print \"Error in reading file\" \n", " break \n", " print ch \n", " \n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Error in reading file\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Standard IO devices, Page number: 472

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import io\n", "\n", "try:\n", " fp = open (\"C:/Users/Akshatha/Documents/extrastuff/skills.txt\", \"r\")\n", "except:\n", " print \"cannot open file\"\n", " exit()\n", " \n", "io.open('stdprn','w') \n", "try:\n", " stdprn = io.open('stdprn', 'w') #open printer\n", "\n", " while ( 1 ):\n", " ch = fp.read(1)\n", " if not ch :\n", " break\n", " else:\n", " stdprn.write(ch) #write into printer\n", "except: #no printer\n", " #closen files\n", " fp.close()\n", " stdprn.close()\n", " " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }