{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 14: Files

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

Example 14.1, Page number: 454

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Write data to text file and read it\n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a')\n", "\n", "#Write data to file if successfully created the file\n", "if fp == 0:\n", " sys.stdout.write(\"Cannot open file\")\n", "else:\n", " sys.stdout.write(\"Write data & to stop press '.'\")\n", " c = ''\n", " while c != '.':\n", " c = raw_input(\"Write data & to stop press '.'\")\n", " fp.write(c)\n", " fp.close()\n", "\n", " #Read and display the contents of file\n", " sys.stdout.write(\"\\nContents read : \")\n", " fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n", " f = fp.readlines()\n", " for f1 in f:\n", " print f1\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Write data & to stop press '.'" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Write data & to stop press '.'ABCDEFGHIJK\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Write data & to stop press '.'.\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Contents read : ABCDEFGHIJK.\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.2, Page number: 455

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display the contents of the file before and after appending.\n", "\n", "import sys\n", "\n", "#Open file for read data\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n", "\n", "#Read and display the contents of file\n", "sys.stdout.write(\"\\nContents of file before appending :\\n\")\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n", "f = fp.readlines()\n", "for f1 in f:\n", " print f1\n", "\n", "#Open file for appending\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a')\n", "\n", "if f == 0:\n", " sys.stdout.write(\"File can not appended\")\n", "else:\n", " c = ''\n", " #fp.write('\\n')\n", " while c != '.':\n", " c = raw_input(\"Enter string to append\")\n", " fp.write(c)\n", " \n", " fp.close()\n", " \n", " #Read and display the contents of file\n", " sys.stdout.write(\"\\nContents of file After appending\\n\")\n", " fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n", " f = fp.readlines()\n", " for f1 in f:\n", " print f1\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Contents of file before appending :\n", "String is terminated with '\\0'.\n", "\n", "Contents of file After appending\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "String is terminated with '\\0'.This character is called as NULL character.\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.3, Page number: 457

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Writing and reading of a file.\n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','w+')\n", "\n", "#Write data to file if successfully created the file\n", "if fp == 0:\n", " sys.stdout.write(\"Cannot open file\")\n", "else:\n", " sys.stdout.write(\"Write data & to stop press '.'\")\n", " c = ''\n", " while c != '.':\n", " c = raw_input(\"Write data & to stop press '.'\")\n", " fp.write(c)\n", " fp.close()\n", "\n", " #Read and display the contents of file\n", " sys.stdout.write(\"\\nContents read : \")\n", " fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n", " f = fp.readlines()\n", " for f1 in f:\n", " print f1\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Write data & to stop press '.'\n", "Contents read : " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "ABCDEFGHIJK.\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.4, Page number: 458

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Open a file in append mode and add new records in it.\n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a+')\n", "\n", "#Write data to file if successfully created the file\n", "if fp == 0:\n", " sys.stdout.write(\"Cannot open file\")\n", "else:\n", " sys.stdout.write(\"Write data & to stop press '.'\")\n", " c = ''\n", " while c != '.':\n", " c = raw_input(\"Write data & to stop press '.'\")\n", " fp.write(c)\n", " fp.close()\n", "\n", " #Read and display the contents of file\n", " sys.stdout.write(\"\\nContents read : \")\n", " fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a+')\n", " f = fp.readlines()\n", " for f1 in f:\n", " print f1\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Write data & to stop press '.'\n", "Contents read : " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "This is append and read mode.\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.5, Page number: 459

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Open a file in read/write mode in it\n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r+')\n", "\n", "#Write data to file if successfully created the file\n", "if fp == 0:\n", " sys.stdout.write(\"Cannot open file\")\n", "else:\n", " #Read and display the contents of file\n", " sys.stdout.write(\"\\nContents read : \")\n", " f = fp.readlines()\n", " for f1 in f:\n", " print f1\n", " \n", " sys.stdout.write(\"Write data & to stop press '.'\")\n", " c = ''\n", " while c != '.':\n", " c = raw_input(\"Write data & to stop press '.'\")\n", " fp.write(c)\n", " fp.close()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Contents read : Help me.\n", "Write data & to stop press '.'" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.6, Page number: 460

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Open a file for read/write operation in binary mode\n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','wb')\n", "\n", "#Write data to file if successfully created the file\n", "if fp == 0:\n", " sys.stdout.write(\"Cannot open file\")\n", "else:\n", " sys.stdout.write(\"Write data & to stop press '.'\")\n", " c = ''\n", " while c != '.':\n", " c = raw_input(\"Write data & to stop press '.'\")\n", " fp.write(c)\n", " fp.close()\n", "\n", " #Read and display the contents of file\n", " sys.stdout.write(\"\\nContents read : \")\n", " fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','rb')\n", " f = fp.readlines()\n", " for f1 in f:\n", " print f1\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Write data & to stop press '.'\n", "Contents read : " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "ABCDEFGHIJK.\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.7, Page number: 462

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Open a text file and write some text \n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','w')\n", "\n", "#Read the string\n", "text = raw_input(\"Enter Text Here : \")\n", "\n", "#Write to the file\n", "#fp.write() is the equivalent function for fprintf() in python\n", "fp.write(\"%s\"%(text))\n", "\n", "#To see the result, open the data.txt file" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.8, Page number: 463

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Enter data into the text file and read the same. \n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','w')\n", "\n", "#Read the data\n", "text = raw_input(\"Name & Age\")\n", "age = int(raw_input(\"Name & Age\"))\n", "\n", "#Write to file\n", "fp.write(\"%s %d\"%(text,age))\n", "\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n", "#Result\n", "sys.stdout.write(\"Name\\tAge\\n\")\n", "text = fp.read()\n", "age = fp.read()\n", "sys.stdout.write(\"%s\\t%s\\n\"%(text,age))\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Name\tAge\n", "AMIT 12\t\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.9, Page number: 463

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read the contents of the file \n", "\n", "import sys\n", "\n", "#Open file\n", "f = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\list.txt','r')\n", "\n", "if f == 0:\n", " sys.stdout.write(\"Cannot open file\")\n", "else:\n", " for c in f:\n", " sys.stdout.write(\"%s\"%(c))\n", " #Python gives iterative statements there is no getc() fucntion" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "aman\n", "akash\n", "amit\n", "ajay\n", "ankit" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.10, Page number: 464

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Write some text into the file \n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\words.doc','w')\n", "\n", "#Write data to file\n", "c = ''\n", "while c != '*':\n", " c = raw_input(\"Enter Few Words '*' to Exit\")\n", " fp.write(c)\n", "\n", "fp.close()\n", " #There is no putc() function in python\n", " #Open the file to see the result" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.11, Page number: 465

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Count\n", "\n", "# Total number of statements\n", "# Total number of included files\n", "# Total number of blocks and brackets\n", "\n", "import sys\n", "\n", "#Open file\n", "fs = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\PRG2.C','r')\n", "\n", "#variable initialization\n", "i = 0\n", "c = 0\n", "sb = 0\n", "b = 0\n", "\n", "if fs == 0:\n", " sys.stdout.write(\"File opening error\")\n", "else:\n", " for line in fs:\n", " k = 0\n", " while k < len(line):\n", " if line[k] == ';':\n", " c += 1\n", " else:\n", " if line[k] == '{':\n", " sb += 1\n", " else:\n", " if line[k] == '(':\n", " b += 1\n", " else:\n", " if line[k] == '#':\n", " i += 1\n", " k += 1\n", " \n", " #Result\n", " sys.stdout.write(\"\\nSummary of 'C' Program\\n\")\n", " sys.stdout.write(\"===========================\")\n", " sys.stdout.write(\"\\nTotal Statments : %d\"%(c+i))\n", " sys.stdout.write(\"\\nInclude Statements : %d\"%(i))\n", " sys.stdout.write(\"\\nTotal Blocks {} : %d\"%(sb))\n", " sys.stdout.write(\"\\nTotal Brackets () : %d\"%(b))\n", " sys.stdout.write(\"\\n============================\")\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Summary of 'C' Program\n", "===========================\n", "Total Statments : 8\n", "Include Statements : 2\n", "Total Blocks {} : 2\n", "Total Brackets () : 9\n", "============================" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.12, Page number: 466

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Write text to a file \n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\lines.txt','w')\n", "\n", "if fp == 0:\n", " sys.stdout.write(\"\")\n", "else:\n", " #Write data to file\n", " c = ''\n", " while c != '*':\n", " c = raw_input(\"\")\n", " fp.write(c)\n", " fp.close()\n", " #There is no fputc() function in python\n", " #Open the file to see the result" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.13, Page number: 467

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read text from the given file \n", "\n", "import sys\n", "\n", "#Open file\n", "\n", "file1 = raw_input(\"Enter File Name : \")\n", "fp = open(file1,'r')\n", "\n", "if fp == 0:\n", " sys.stdout.write(\"File not found\\n\")\n", "else:\n", " for text in fp:\n", " print text\n", " #File pointer itself is iterable in python" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "#include \n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.14, Page number: 468

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Write a string into a file \n", "\n", "import sys\n", "\n", "#Open file\n", "\n", "file1 = raw_input(\"Enter the name of file : \")\n", "fp = open(file1,'w')\n", "\n", "if fp == 0:\n", " sys.stdout.write(\"File can not opened\\n\")\n", "else:\n", " text = raw_input(\"Enter Text Here : \")\n", " fp.write(text)\n", " \n", " #Write() function is used to write character or string into the file in python\n", " # there is no fputc() function in python\n", "#open the file to see the result" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.15, Page number: 469

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Write integers in a file \n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open('num.txt','w')\n", "\n", "if fp == 0:\n", " sys.stdout.write(\"File does not exist\\n\")\n", "else:\n", " v = ' '\n", " while v != '0':\n", " fp.write(v)\n", " v = raw_input(\"Enter Numbers\")\n", " \n", " #open the file to see the result" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.16, Page number: 470

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read integers from the file \n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open('num.txt','r')\n", "\n", "if fp == 0:\n", " sys.stdout.write(\"File does not exist\\n\")\n", "else:\n", " for v in fp:\n", " print v" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 3 4 5\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.17, Page number: 471

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Write a block of structure elements to the given file \n", "\n", "import sys\n", "\n", "#Class declaration\n", "class student:\n", " name = ''\n", " age = 0\n", " \n", "#Class variable declaration\n", "stud = [student() for i in range(0,5)]\n", "\n", "#Open file\n", "file1 = raw_input(\"Enter the file name : \")\n", "fp = open(file1,'w')\n", "\n", "if fp == 0:\n", " sys.stdout.write(\"File does not exist\\n\")\n", "else:\n", " n = int(raw_input(\"How Many Records : \"))\n", " for i in range(0,n):\n", " stud[i].name = raw_input(\"Name : \")\n", " stud[i].age = int(raw_input(\"Age : \"))\n", " \n", " j = 0\n", " while j < n:\n", " fp.write(\"%s %d\\n\"%(stud[j].name,stud[j].age))\n", " j += 1\n", " \n", " fp.close()\n", " \n", " #open the file to see the result" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.18, Page number: 472

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Write and read the information about the player \n", "\n", "import sys\n", "\n", "#Class declaration\n", "class record:\n", " player = ''\n", " age = 0\n", " runs = 0\n", " \n", "#Class variable declaration\n", "emp = record()\n", "\n", "#Open file\n", "fp = open('record.dat','w')\n", "\n", "if fp == 0:\n", " sys.stdout.write(\"Can not open the file\\n\")\n", "else:\n", " emp.player = raw_input(\"Enter Player Name\")\n", " emp.age = int(raw_input(\"Enter Age\"))\n", " emp.runs = int(raw_input(\"Enter runs scored\"))\n", " fp.write(\"%s %d %d\"%(emp.player,emp.age,emp.runs))\n", " fp.close()\n", " \n", " fp = open('record.dat','r')\n", " if fp == 0:\n", " sys.stdout.write(\"Error in opening file\")\n", " else:\n", " sys.stdout.write(\"\\nRecord Entered is\\n\")\n", " emp.player,emp.age,emp.runs = fp.read().split(' ')\n", " sys.stdout.write(\"\\n%s %s %s\"%(emp.player,emp.age,emp.runs))\n", " fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Player NameSachin\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Age25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter runs scored10000\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Record Entered is\n", "\n", "Sachin 25 10000" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.19, Page number: 473

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Write a block of structure elements to the given file \n", "\n", "import sys\n", "\n", "#Variable Initialization\n", "next1 = 'Y'\n", "\n", "#Class declaration\n", "class bike:\n", " name = ''\n", " avg = 0\n", " cost = 0.0\n", " \n", "#Class variable declaration\n", "e = bike()\n", "\n", "#open file\n", "fp = open('bk.txt','wb')\n", "\n", "if fp == 0:\n", " sys.stdout.write(\"Cannot open file\")\n", "else:\n", " while next1 == 'Y':\n", " e.name = raw_input(\"Model Name\")\n", " e.avg = int(raw_input(\"Average\"))\n", " e.cost = float(raw_input(\"Price\"))\n", " fp.write(\"%s %d %f\\n\"%(e.name,e.avg,e.cost))\n", " \n", " next1 = raw_input(\"Add Another (Y/N):\")\n", " fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Model NameHONDA\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Average80\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Price45000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Add Another (Y/N):Y\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Model NameSUZUKI\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Average65\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Price43000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Add Another (Y/N):Y\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Model NameYAMAHA\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Average55\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Price48000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Add Another (Y/N):N\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.20, Page number: 474

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read the information about the bike \n", "\n", "import sys\n", "\n", "#Class declaration\n", "class bike:\n", " name = ''\n", " avg = 0\n", " cost = 0.0\n", " \n", "#Class variable declaration\n", "e = bike()\n", "\n", "#open file\n", "fp = open('bk.txt','rb')\n", "\n", "if fp == 0:\n", " sys.stdout.write(\"Cannot open file\")\n", "else:\n", " lines = fp.readlines()\n", " for line in lines:\n", " e.name,e.avg,e.cost = line.split(' ')\n", " e.avg = int(e.avg)\n", " e.cost = float(e.cost)\n", " sys.stdout.write(\"\\n%s %d %.2f\"%(e.name,e.avg,e.cost))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "HONDA 80 45000.00\n", "SUZUKI 65 43000.00\n", "YAMAHA 55 48000.00" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.21, Page number: 476

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read the text after skipping n characters from beginning of the file\n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open(\"text.txt\",\"r\")\n", "\n", "sys.stdout.write(\"\\nContents of file\\n\")\n", "ch = fp.read()\n", "sys.stdout.write(\"%s\"%(ch))\n", "\n", "#Read n\n", "n = int(raw_input(\"How many characters including spaces would you like to skip?\"))\n", "\n", "#Result\n", "sys.stdout.write(\"\\nInformation after %d bytes\\n\"%(n))\n", "\n", "ch = ch[n:]\n", "sys.stdout.write(\"%s\"%(ch))\n", "\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Contents of file\n", "THE C PROGRAMMING LANGUAGE INVENTED BY DENNIS RITCHIE" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "How many characters including spaces would you like to skip?18\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Information after 18 bytes\n", "LANGUAGE INVENTED BY DENNIS RITCHIE" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.22, Page number: 477

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read last few characters of the file \n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open(\"text.txt\",\"r\")\n", "\n", "sys.stdout.write(\"\\nContents of file\\n\")\n", "ch = fp.read()\n", "sys.stdout.write(\"%s\"%(ch))\n", "\n", "#Read n\n", "n = int(raw_input(\"How many characters including spaces would you like to skip?\"))\n", "\n", "#Result\n", "sys.stdout.write(\"\\nInformation after %d bytes\\n\"%(n))\n", "\n", "print ch[-n:]\n", "\n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Contents of file\n", "HELLO WORLD" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "How many characters including spaces would you like to skip?5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Information after 5 bytes\n", "WORLD\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.23, Page number: 477

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display 'c' program files in current directory. \n", "\n", "import sys\n", "\n", "#Variable initialization\n", "l = 0\n", "c = 0\n", "\n", "#Open file\n", "file1 = raw_input(\"Enter the file name : \")\n", "fp = open(file1,'r')\n", "\n", "#Result\n", "sys.stdout.write(\"\\nContents of 'c' program file in capital case\")\n", "sys.stdout.write(\"\\n============================================\\n\")\n", "\n", "ch = fp.readlines()\n", "\n", "for line in ch:\n", " i = 0\n", " #print line.upper()\n", " while i < len(line):\n", " if line[i] =='\\n':\n", " l += 1\n", " else:\n", " c += 1\n", " sys.stdout.write(\"%c\"%(line[i].upper()))\n", " i += 1\n", "\n", "sys.stdout.write(\"\\nTotal Characters : %d\"%(c))\n", "sys.stdout.write(\"\\nTotal Lines : %d\"%(l))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Contents of 'c' program file in capital case\n", "============================================\n", "MAIN()\n", "{\n", "PRINTF(\" HELLO WORLD\");\n", "}\n", "\n", "Total Characters : 31\n", "Total Lines : 4" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.24, Page number: 479

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Detect the end of file \n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open(\"text.txt\",\"r\")\n", "\n", "#there is no feof() function in python\n", "c = fp.tell()\n", "sys.stdout.write(\"File pointer at the beginning of the file : %d\\n\"%(c))\n", "\n", "c = fp.read()\n", "sys.stdout.write(\"%s\"%(c))\n", "\n", "c = fp.tell()\n", "sys.stdout.write(\"\\nFile pointer at the end of file : %d\"%(c))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "File pointer at the beginning of the file : 0\n", "TECHNOCRATS LEAD THE WORLD \n", "File pointer at the end of file : 32" ] } ], "prompt_number": 26 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.25, Page number: 480

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Detect an error while read/write operation of a file is in use.\n", "\n", "import sys\n", "\n", "#Variable initialization\n", "next1 = 'Y'\n", "\n", "#open file\n", "fp = open('marks.dat','r')\n", "\n", "if fp == 0:\n", " sys.stdout.write(\"Can not open file\")\n", "else:\n", " while next1 == 'Y':\n", " name = raw_input(\"Enter Name, Marks, Percentage\")\n", " marks = int(raw_input(\"Enter Name, Marks, Percentage\"))\n", " \n", " p = marks/7\n", " try:\n", " fp.write(\"%s %d %f\"%(name,marks,p))\n", " except:\n", " sys.stdout.write(\"\\nUnable to write data?\")\n", " sys.stdout.write(\"\\nFile opening mode is incorrect.\")\n", " fp.close()\n", " \n", " next1 = raw_input(\"Continue Y/N:\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Name, Marks, PercentageKAMAL\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Name, Marks, Percentage540\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Unable to write data?\n", "File opening mode is incorrect." ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Continue Y/N:N\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.26, Page number: 481

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Catch the error that is occurred while file operation\n", "\n", "import sys\n", "\n", "#Open file\n", "f = open(\"io8.c\",\"w\")\n", "\n", "if f == 0:\n", " sys.stdout.write(\"\\nCannot open file\")\n", "else:\n", " #Exception handling\n", " try:\n", " c = fp.readlines()\n", " sys.stdout.write(\"%s\"%(c))\n", " except:\n", " sys.stdout.write(\"\\nCan't read file.\")\n", "#There is no ferror() function python." ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Can't read file." ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.27, Page number: 482

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Detect and print the error message\n", "\n", "import sys\n", "\n", "#Variable Initialization\n", "file1 = \"lines.txt\"\n", "\n", "#Open file\n", "fr = open(file1,\"w\")\n", "\n", "sys.stdout.write(\"%s : \"%(file1))\n", "#Exception handling\n", "try:\n", " c = fp.readlines()\n", "except:\n", " sys.stdout.write(\"Permission Denied\")\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "lines.txt : Permission Denied" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.28, Page number: 482

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Print the current position of the file pointer \n", "\n", "import sys\n", "\n", "#Open file\n", "fp = open(\"text.txt\",\"r\")\n", "\n", "#Set the pointer\n", "fp.seek(21)\n", "\n", "#Result\n", "while True:\n", " c = fp.read(1)\n", " if not c:\n", " break\n", " sys.stdout.write(\"%c\\t%d\\n\"%(c,fp.tell()))\n", "#There is no endof file in python" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "W\t22\n", "O\t23\n", "R\t24\n", "L\t25\n", "D\t26\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.29, Page number: 483

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Detect beginning of file\n", "\n", "import sys\n", "\n", "#open file\n", "fp = open(\"text.txt\",\"r\")\n", "\n", "fp.seek(12)\n", "sys.stdout.write(\"Pointer is at %d\\n\"%(fp.tell()))\n", "sys.stdout.write(\"Before rewind() : \")\n", "\n", "#Result\n", "while True:\n", " c = fp.read(1)\n", " if not c:\n", " break\n", " sys.stdout.write(\"%c\"%(c))\n", " \n", "sys.stdout.write(\"\\nAfter rewind() : \")\n", "fp.seek(0)\n", "#There is no rewind function in python\n", "\n", "while True:\n", " c = fp.read(1)\n", " if not c:\n", " break\n", " sys.stdout.write(\"%c\"%(c))\n", " \n", "fp.close()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Pointer is at 12\n", "Before rewind() : LEAD THE WORLD\n", "After rewind() : TECHNOCRATS LEAD THE WORLD" ] } ], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.30, Page number: 484

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Delete the given file \n", "\n", "import sys\n", "import os\n", "\n", "#Read file name\n", "file1 = raw_input(\"Enter The File Name : \")\n", "\n", "#There is no remove or unlink file function in python.\n", "#A file can be deleted using remove function in the os module.\n", "\n", "try:\n", " os.remove(file1)\n", " sys.stdout.write(\"\\nFile (%s) has been deleted!\"%(file1))\n", "except:\n", " sys.stdout.write(\"\\nFile does not exist\")\n", "\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "File (TEXT.TXT) has been deleted!" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.31, Page number: 485

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Change the name of the file\n", "\n", "import os\n", "import sys\n", "\n", "old = raw_input(\"Old File Name : \")\n", "\n", "new = raw_input(\"New File Name : \")\n", "\n", "os.rename(old, new)\n", "\n", "#Check the directory to see the result\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.32, Page number: 486

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Copy the contents of one file to another file\n", "\n", "import sys\n", "\n", "fs = open(\"a.txt\",\"r\")\n", "ft = open(\"b.txt\",\"w\")\n", "c = 0\n", "\n", "if fs == 0:\n", " sys.stdout.write(\"\\nSource file opening error.\")\n", "else:\n", " if ft == 0:\n", " sys.stdout.write(\"\\nTarget file opening error.\")\n", " else:\n", " while True:\n", " ch = fs.read(1)\n", " if not ch:\n", " break\n", " ft.write(\"%c\"%(ch))\n", " c += 1\n", " sys.stdout.write(\"\\n%d Bytes copied from 'a.txt' to 'b.txt'.\"%(c))\n", " c = 0\n", " #there is no fcloseall() function in python\n", " fs.close()\n", " c += 1\n", " ft.close()\n", " c += 1\n", " sys.stdout.write(\"\\n%d files closed.\"%(c))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "45 Bytes copied from 'a.txt' to 'b.txt'.\n", "2 files closed." ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.33, Page number: 487

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read the contents of three files and find the largest file\n", "\n", "import sys\n", "\n", "#Variable Initialization\n", "y = 0\n", "k = 0\n", "t = 0\n", "name = [\"1.txt\",\"2.txt\",\"3.txt\"]\n", "f = [0 for i in range(0,3)]\n", "x = [0 for i in range(0,3)]\n", "\n", "#Open all the files\n", "for l in range(0,3):\n", " fp = open(name[l],\"r\")\n", " f[l] = fp\n", " if fp == 0:\n", " sys.stdout.write(\"\\n%s file not found.\"%(name[l]))\n", " break\n", " \n", "#Read contents of all files\n", "for l in range(0,3):\n", " while True:\n", " c1 = f[l].read(1)\n", " if not c1:\n", " break\n", " x[l] = y\n", " y += 1\n", " y = 0\n", "\n", "#close the files\n", "for l in range(0,3):\n", " f[l].close()\n", "\n", "#Print size of all files\n", "for l in range(0,2+1):\n", " sys.stdout.write(\"File : %s Bytes : %d\\n\"%(name[l],x[l]))\n", " t = t + x[l]\n", " \n", "#Find largest\n", "for l in range(t,1,-1):\n", " for k in range(0,3):\n", " if l == x[k]:\n", " sys.stdout.write(\"\\n%s are the largest file.\"%(name[k]))\n", " break\n", " if l == x[k]:\n", " break" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "File : 1.txt Bytes : 16\n", "File : 2.txt Bytes : 20\n", "File : 3.txt Bytes : 25\n", "\n", "3.txt are the largest file." ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.34, Page number: 488

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Copy 100 characters from a file to an array\n", "\n", "import sys\n", "\n", "#Variable Initialization\n", "SIZE = 100\n", "s = 0\n", "x = 0\n", "ch = ['0' for i in range(0,100)]\n", "\n", "#Open the files\n", "f = open(\"poem.txt\",\"r\")\n", "f2 = open(\"alpha.txt\",\"w\")\n", "\n", "if f ==0 or f2 == 0:\n", " sys.stdout.write(\"?\")\n", "else:\n", " while True:\n", " c = f.read(1)\n", " x += 1\n", " if not c:\n", " break\n", " else:\n", " if x == 99:\n", " break\n", " else:\n", " ch[s] = c\n", " s += 1\n", "\n", "for s in range(0,100):\n", " f2.write(\"%c\"%(ch[s]))\n", "\n", "sys.stdout.write(\"Process Completed : Error 0\")\n", "f.close()\n", "f2.close()\n", "\n", "#There is no perror() function in python" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Process Completed : Error 0" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.35, Page number: 491

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Low level disk I/O operations.\n", "\n", "import sys\n", "\n", "\n", "#Variable Initialization\n", "file1 = raw_input(\"Enter a file name : \")\n", "\n", "#Open file\n", "s = open(file1,\"w\")\n", "\n", "#Result\n", "if s == -1:\n", " sys.stdout.write(\"File does not exits\")\n", "else:\n", " buff = raw_input(\"Enter text below:\")\n", " s.write(\"%s\"%(buff))\n", " s.close()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a file name : TEXT\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter text below:PROGRAMMING IN C\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.36, Page number: 492

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read text from a specified file \n", "\n", "import sys\n", "\n", "#Variable initialization\n", "file1 = raw_input(\"Enter a file name \")\n", "\n", "#open file\n", "s = open(file1,\"r\")\n", "\n", "#Result\n", "if s == -1:\n", " sys.stdout.write(\"File does not exists\")\n", "else:\n", " while True:\n", " ch = s.read(1)\n", " if not ch:\n", " break\n", " sys.stdout.write(\"%c\"%(ch))\n", " \n", "s.close()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a file name TEXT\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "PROGRAMMING IN C" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.37, Page number: 493

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Set a buffer size \n", "\n", "import sys\n", "\n", "#Result\n", "sys.stdout.write(\"\\nThis book teaches C\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "This book teaches C" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.38, Page number: 494

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display number of arguments and their names\n", "\n", "import sys\n", "\n", "#Result\n", "sys.stdout.write(\"\\nTotal number of arguments are %d\\n\"%(len(sys.argv)))\n", "\n", "print str(sys.argv)\n", "\n", "#Command line arguments can be given in python by the command\n", "\n", "# python pgmname.py arg1 arg2 arg3\n", "\n", "#This is not possible in ipython notebook" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.39, Page number: 495

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read any file from command prompt\n", "\n", "import sys\n", "\n", "#open file\n", "fp = open(sys.argv[1],\"r\")\n", "\n", "#Result\n", "if fp == 0:\n", " sys.stdout.write(\"Can not open file\")\n", "else:\n", " while True:\n", " ch = fp.read(1)\n", " if not ch:\n", " break\n", " sys.stdout.write(\"%c\"%(ch))\n", " \n", "fp.close()\n", "\n", "#This program can be run in python as\n", "\n", "# python pgmname.py filename" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.40, Page number: 495

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Command line argument to perform the task of DEL command\n", "\n", "import sys\n", "\n", "#Check number of arguments\n", "if len(sys.argv) < 2:\n", " sys.stdout.write(\"Insufficient Arguments\")\n", "else:\n", " fp = open(sys.argv[1],\"r\")\n", " if fp == 0:\n", " sys.stdout.write(\"File Not Found\")\n", " fp.close()\n", " os.remove(sys.argv[1])\n", " sys.stdout.write(\"File has been deleted\")\n", " \n", "#This program can be deleted using\n", "# python pgmname.py filename\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.41, Page number: 496

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Command line argument to perform the task of REN command\n", "\n", "import sys\n", "\n", "#Check number of arguments\n", "if len(sys.srgv) < 3:\n", " sys.stdout.write(\"Insufficient Arguments\")\n", "else:\n", " fp = open(sys.argv[1],\"r\")\n", " if fp == 0:\n", " sys.stdout.write(\"File Not Found\")\n", " else:\n", " sp = open(sys.argv[2],\"r\")\n", " if sp == 0:\n", " fp.close()\n", " sp.close()\n", " #Rename file\n", " os.rename(sys.argv[1],sys.argv[2])\n", " else:\n", " sys.stdout.write(\"Duplicate file name or file is in use\")\n", " \n", "#This program can be executed as\n", "\n", "# python pgmname.py oldfilename newfilename\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.42, Page number: 497

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Environment variable and display the various settings.\n", "\n", "import sys\n", "import os\n", "\n", "#Result\n", "print os.environ\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'TMP': 'C:\\\\Users\\\\Aathira\\\\AppData\\\\Local\\\\Temp', 'COMPUTERNAME': 'AATHIRA-PC', 'GUROBI_HOME': 'B:\\\\gurobi510\\\\win32', 'USERDOMAIN': 'Aathira-PC', 'PSMODULEPATH': 'C:\\\\Windows\\\\system32\\\\WindowsPowerShell\\\\v1.0\\\\Modules\\\\', 'COMMONPROGRAMFILES': 'C:\\\\Program Files (x86)\\\\Common Files', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 15 Stepping 13, GenuineIntel', 'PROGRAMFILES': 'C:\\\\Program Files (x86)', 'PROCESSOR_REVISION': '0f0d', 'SYSTEMROOT': 'C:\\\\Windows', 'PATH': 'C:\\\\Anaconda\\\\lib\\\\site-packages\\\\numpy\\\\core;B:\\\\gurobi510\\\\win32\\\\bin;C:\\\\Windows\\\\system32;C:\\\\Windows;C:\\\\Windows\\\\System32\\\\Wbem;C:\\\\Windows\\\\System32\\\\WindowsPowerShell\\\\v1.0\\\\;C:\\\\Program Files (x86)\\\\Common Files\\\\Adobe\\\\AGL;C:\\\\Program Files\\\\MATLAB\\\\R2009a\\\\bin;C:\\\\Program Files\\\\MATLAB\\\\R2009a\\\\bin\\\\win64;c:\\\\python27\\\\scripts;C:\\\\Program Files (x86)\\\\MiKTeX 2.9\\\\miktex\\\\bin\\\\;C:\\\\Anaconda;C:\\\\Anaconda\\\\Scripts;C:\\\\Program Files (x86)\\\\ffmpeg', 'CLICOLOR': '1', 'PROGRAMFILES(X86)': 'C:\\\\Program Files (x86)', 'COMSPEC': 'C:\\\\Windows\\\\system32\\\\cmd.exe', 'TK_LIBRARY': 'C:\\\\Anaconda\\\\tcl\\\\tk8.5', 'TERM': 'xterm-color', 'TEMP': 'C:\\\\Users\\\\Aathira\\\\AppData\\\\Local\\\\Temp', 'COMMONPROGRAMFILES(X86)': 'C:\\\\Program Files (x86)\\\\Common Files', 'PROCESSOR_ARCHITECTURE': 'x86', 'TIX_LIBRARY': 'C:\\\\Anaconda\\\\tcl\\\\tix8.4.3', 'ALLUSERSPROFILE': 'C:\\\\ProgramData', 'LOCALAPPDATA': 'C:\\\\Users\\\\Aathira\\\\AppData\\\\Local', 'HOMEPATH': '\\\\Users\\\\Aathira', 'PROGRAMW6432': 'C:\\\\Program Files', 'USERNAME': 'Aathira', 'LOGONSERVER': '\\\\\\\\AATHIRA-PC', 'SESSIONNAME': 'Console', 'PROGRAMDATA': 'C:\\\\ProgramData', 'TCL_LIBRARY': 'C:\\\\Anaconda\\\\tcl\\\\tcl8.5', 'GIT_PAGER': 'cat', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'FP_NO_HOST_CHECK': 'NO', 'WINDIR': 'C:\\\\Windows', 'APPDATA': 'C:\\\\Users\\\\Aathira\\\\AppData\\\\Roaming', 'HOMEDRIVE': 'C:', 'PAGER': 'cat', 'SYSTEMDRIVE': 'C:', 'NUMBER_OF_PROCESSORS': '2', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_ARCHITEW6432': 'AMD64', 'COMMONPROGRAMW6432': 'C:\\\\Program Files\\\\Common Files', 'OS': 'Windows_NT', 'PUBLIC': 'C:\\\\Users\\\\Public', 'USERPROFILE': 'C:\\\\Users\\\\Aathira'}\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.43, Page number: 498

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read character from keyboard \n", "\n", "import sys\n", "\n", "c = '0'\n", "\n", "#Result\n", "while c != ' ':\n", " c = raw_input(\"\")\n", " sys.stdout.write(\"%c \"%(c))\n", " \n", "#Give space at the end" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "3 4 " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "5 6 " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "7 8 " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "9 " ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 14.44, Page number: 499

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display A to Z characters\n", "\n", "import sys\n", "\n", "#Result\n", "for a in range(65,91):\n", " sys.stdout.write(\"%c\\t\"%(chr(a)))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A\tB\tC\tD\tE\tF\tG\tH\tI\tJ\tK\tL\tM\tN\tO\tP\tQ\tR\tS\tT\tU\tV\tW\tX\tY\tZ\t" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }