{ "metadata": { "name": "Chapter XVII" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 17: Miscellaneous and Advanced Features" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 17.1, Page number: 382" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "\n", "def main():\n", " \n", " #Un-Comment this while execution from terminal/Command Line\n", " #if(len(sys.argv)!=3):\n", " #print(\"need two file names!\")\n", " #sys.exit()\n", " \n", " inName=\"source.txt\" #str(sys.argv[1])\n", " outName=\"target.txt\" #str(sys.argv[2])\n", "\n", " #Try to open a file for reading\n", " try:\n", " inn=open(inName,\"r\") \n", " except:# Exception:\n", " print(\"cant open {0} for reading\".format(inName))\n", " sys.exit()\n", "\n", " try:\n", " out=open(outName,\"w\") \n", " except:# Exception:\n", " print(\"cant open {0} for writing\".format(outName))\n", " sys.exit()\n", "\n", " string=inn.read() #Read content from File-1\n", " out.write(string) #Write content to File-2\n", "\n", " inn.close()\n", " out.close()\n", "\n", "\n", " print(\"File has been copied.\\n\");\n", "\n", "\n", "if __name__=='__main__':\n", " main()\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "SystemExit", "evalue": "", "output_type": "pyerr", "traceback": [ "An exception has occurred, use %tb to see the full traceback.\n", "\u001b[0;31mSystemExit\u001b[0m\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "cant open source.txt for reading\n" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "To exit: use 'exit', 'quit', or Ctrl-D." ] } ], "prompt_number": 1 } ], "metadata": {} } ] }