diff options
author | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
commit | 206d0358703aa05d5d7315900fe1d054c2817ddc (patch) | |
tree | f2403e29f3aded0caf7a2434ea50dd507f6545e2 /Programming_in_C/Chapter_17.ipynb | |
parent | c6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff) | |
download | Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2 Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip |
adding book
Diffstat (limited to 'Programming_in_C/Chapter_17.ipynb')
-rw-r--r-- | Programming_in_C/Chapter_17.ipynb | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/Programming_in_C/Chapter_17.ipynb b/Programming_in_C/Chapter_17.ipynb new file mode 100644 index 00000000..192aef85 --- /dev/null +++ b/Programming_in_C/Chapter_17.ipynb @@ -0,0 +1,110 @@ +{ + "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": [ + "#17.1.py\n", + "#File Copy Program Using Command-Line Arguments\n", + "\n", + "#Import Libraries\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", + "#Read Command Line Arguments\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 to open a file for writing\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", + "#Close Files\n", + " inn.close()\n", + " out.close()\n", + "\n", + "\n", + " print(\"File has been copied.\\n\");\n", + "\n", + "\n", + "#Setting top level conditional script\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": {} + } + ] +}
\ No newline at end of file |