{ "metadata": { "name": "", "signature": "sha256:619858f96c40102945d24a70a3fd99304f6fb166114144600528626d39e31490" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 22 : Using Special File Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 22.1, Page No 375" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def PtrSeek(fptr):\n", " offset1 = PtrTell(fptr)\n", " DataRead(fptr)\n", " offset2 = PtrTell(fptr)\n", " DataRead(fptr)\n", " offset3 = PtrTell(fptr)\n", " DataRead(fptr)\n", " print \"\\nRe-read the haiku:\\n\"\n", " fptr.seek(offset3)\n", " DataRead(fptr)\n", " fptr.seek(offset2)\n", " DataRead(fptr)\n", " fptr.seek(offset1)\n", " DataRead(fptr)\n", "def PtrTell(fprt):\n", " reval = fptr.tell()\n", " print \"The fptr is at: \",reval,\"\\n\"\n", " return reval\n", "def DataRead(fptr):\n", " buff = range(80)\n", " buff = fptr.readline()\n", " print buff\n", "def ErrorMsg(str):\n", " print \"Cannot open\",str,\"\\n\"\n", " return FAIL\n", "MAX_LEN = 80\n", "try:\n", " filename = \"haiku.txt\"\n", " reval = \"SUCCESS\"\n", " fptr = open(filename,\"r\")\n", " if(not isinstance(fptr,file)):\n", " reval = ErrorMsg(filename)\n", " else:\n", " PtrSeek(fptr)\n", " fptr.close()\n", "except IOError:\n", " print \"\\nFile can not be opened\\n\"\n", " \n", " \n", "\"\"\"\n", "haiku.txt (Read file)\n", "Leading me along\n", "my shadow goes back home\n", "from looking at the moon.\n", "--- Sodo\n", "(1641-1716)\n", "A storm wind blows\n", "out from among the grasses\n", "the full moon grows.\n", "--- Chora\n", "(1729-1781)\n", "\"\"\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The fptr is at: 0 \n", "\n", "Leading me along\n", "\n", "The fptr is at: 18 \n", "\n", "my shadow goes back home\n", "\n", "The fptr is at: 44 \n", "\n", "from looking at the moon.\n", "\n", "\n", "Re-read the haiku:\n", "\n", "from looking at the moon.\n", "\n", "my shadow goes back home\n", "\n", "Leading me along\n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 22.2, Page No 379" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "MAX_NUM = 3\n", "def DataWrite(fout):\n", " buff = [123.45,567.89,100.11]\n", " print \"The size of buff:\",sys.getsizeof(buff),\"-byte\\n\u201d, sizeof(buff)\"\n", " for i in range(MAX_NUM):\n", " print buff[i],\"\\n\"\n", " fout.write(str(buff[i]) + \"\\n\")\n", "def DataRead(fin):\n", " print \"\\nRead back from the binary file:\\n\"\n", " for i in range(MAX_NUM):\n", " x = fin.readline()\n", " print x,\"\\n\"\n", "def ErrorMsg(str):\n", " print \"Cannot open\",str,\"\\n\"\n", " return FAIL\n", "try:\n", " filename = \"double.bin\"\n", " reval = \"SUCCESS\"\n", " fptr = open(filename,\"wb\")\n", " if(not isinstance(fptr,file)):\n", " reval = ErrorMsg(filename)\n", " else:\n", " DataWrite(fptr)\n", " # rewind(fptr) No Such Function In Python\n", " fptr.close()\n", " fptr = open(filename,\"r\")\n", " DataRead(fptr)\n", " fptr.close()\n", "except IOError:\n", " print \"\\nFile can not be opened\\n\"\n", "\n", "\"\"\"\n", "double.bin (Writing Binary File)\n", "123.45\n", "567.89\n", "100.11\n", "\"\"\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The size of buff: 88 -byte\n", "\u201d, sizeof(buff)\n", "123.45 \n", "\n", "567.89 \n", "\n", "100.11 \n", "\n", "\n", "Read back from the binary file:\n", "\n", "123.45\n", "\n", "\n", "567.89\n", "\n", "\n", "100.11\n", "\n", "\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 22.3, Page No 382" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "MAX_NUM = 3\n", "STR_LEN = 23\n", "def DataWrite(fout):\n", " cities = range(MAX_NUM)\n", " cities = [\"St.Louis->Houston:\",\"Houston->Dallas:\",\"Dallas->Philadelphia:\"]\n", " miles = range(MAX_NUM)\n", " miles = [845,243,1459]\n", " print \"The data written:\\n\"\n", " for i in range(MAX_NUM):\n", " print cities[i],miles[i],\"\\n\"\n", " fout.writelines(str(cities[i]) + str(miles[i]) + \"\\n\")\n", "def DataRead(fin):\n", " print \"\\nThe data read:\\n\"\n", " for i in range(MAX_NUM):\n", " cities = fin.read()\n", " miles = fin.read()\n", " print cities,miles\n", "def ErrorMsg(str):\n", " print \"Cannot open\",str,\"\\n\"\n", " return FAIL\n", "try:\n", " filename = \"strnum.mix\"\n", " reval = \"SUCCESS\"\n", " fptr = open(filename,\"w+\")\n", " if(not isinstance(fptr,file)):\n", " reval = ErrorMsg(filename)\n", " else:\n", " DataWrite(fptr)\n", " # rewind(fptr) No Such Function In Python\n", " fptr.close()\n", " fptr = open(filename,\"r\")\n", " DataRead(fptr)\n", " fptr.close()\n", "except IOError:\n", " print \"\\nFile can not be opened\\n\"\n", "\"\"\"\n", "strnum.mix (Writing and Reading File)\n", "St.Louis->Houston:845\n", "Houston->Dallas:243\n", "Dallas->Philadelphia:1459\n", "\"\"\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The data written:\n", "\n", "St.Louis->Houston: 845 \n", "\n", "Houston->Dallas: 243 \n", "\n", "Dallas->Philadelphia: 1459 \n", "\n", "\n", "The data read:\n", "\n", "St.Louis->Houston:845\n", "Houston->Dallas:243\n", "Dallas->Philadelphia:1459\n", "\n", " \n", " \n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 22.4, Page No 385" ] }, { "cell_type": "code", "collapsed": false, "input": [ "STR_NUM = 4\n", "def ErrorMsg(str1):\n", " print \"Cannot open\",str,\"\\n\"\n", " return FAIL\n", "def StrPrint(str1):\n", " for i in range(STR_NUM):\n", " print str1[i],\"\\n\"\n", " stdout.writelines(str1[i] + \"\\n\")\n", " \n", "try:\n", " str1 = range(STR_NUM)\n", " str1 = [\"Be bent, and you will remain straight.\",\"Be vacant, and you will remain full.\",\"Be worn, and you will remain new.\",\"--- by Lao Tzu\"]\n", " filename = \"LaoTzu.txt\"\n", " reval = \"SUCCESS\"\n", " stdout = open(filename,\"w+\")\n", " StrPrint(str1)\n", " stdout.close()\n", " stdout = open(filename,\"w+\")\n", " if(not isinstance(stdout,file)):\n", " reval = ErrorMsg(filename)\n", " else:\n", " StrPrint(str1)\n", " stdout.close()\n", "except IOError:\n", " print \"\\nFile can not be opened\\n\"\n", "\n", "\"\"\"\n", " Output File : LaoTzu.txt\n", " \n", " Be bent, and you will remain straight.\n", "Be vacant, and you will remain full.\n", "Be worn, and you will remain new.\n", "--- by Lao Tzu\n", "\n", "\n", "\"\"\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Be bent, and you will remain straight. \n", "\n", "Be vacant, and you will remain full. \n", "\n", "Be worn, and you will remain new. \n", "\n", "--- by Lao Tzu \n", "\n", "Be bent, and you will remain straight. \n", "\n", "Be vacant, and you will remain full. \n", "\n", "Be worn, and you will remain new. \n", "\n", "--- by Lao Tzu \n", "\n" ] } ], "prompt_number": 4 } ], "metadata": {} } ] }