summaryrefslogtreecommitdiff
path: root/The_Spirit_of_C/Chapter14.ipynb
diff options
context:
space:
mode:
authorhardythe12015-05-05 14:21:39 +0530
committerhardythe12015-05-05 14:21:39 +0530
commit435840cef00c596d9e608f9eb2d96f522ea8505a (patch)
tree4c783890c984c67022977ca98432e5e4bab30678 /The_Spirit_of_C/Chapter14.ipynb
parentaa1863f344766ca7f7c20a395e58d0fb23c52130 (diff)
downloadPython-Textbook-Companions-435840cef00c596d9e608f9eb2d96f522ea8505a.tar.gz
Python-Textbook-Companions-435840cef00c596d9e608f9eb2d96f522ea8505a.tar.bz2
Python-Textbook-Companions-435840cef00c596d9e608f9eb2d96f522ea8505a.zip
add books
Diffstat (limited to 'The_Spirit_of_C/Chapter14.ipynb')
-rwxr-xr-xThe_Spirit_of_C/Chapter14.ipynb298
1 files changed, 298 insertions, 0 deletions
diff --git a/The_Spirit_of_C/Chapter14.ipynb b/The_Spirit_of_C/Chapter14.ipynb
new file mode 100755
index 00000000..380e83ef
--- /dev/null
+++ b/The_Spirit_of_C/Chapter14.ipynb
@@ -0,0 +1,298 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:3748f84e3bd0279efe47855bd2c4e23912ef725121f3591980fa33e19d6ccae4"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 14, Input/Output and Files"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 14-5, Page Number: 424"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Files : Opening and Closing\n",
+ "\n",
+ "# Open the file for writing\n",
+ "\n",
+ "file_pointer = open(\"first_file\",\"w\")\n",
+ "\n",
+ "print \"Enter text:\"\n",
+ "\n",
+ "# Get characters and place them in the file\n",
+ "\n",
+ "text = \"BASIC is easy,\\nFORTAN is fun,\\nPascal may be structured,\\nBut C's the one!\"\n",
+ "print text\n",
+ "file_pointer.write(text)\n",
+ "\n",
+ "file_pointer.close()\n",
+ "\n",
+ "file_pointer = open(\"first_file\",\"r\")\n",
+ "\n",
+ "#Display the text from file\n",
+ "\n",
+ "print \"\\nYou entered:\"\n",
+ "print file_pointer.read()\n",
+ "\n",
+ "file_pointer.close()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter text:\n",
+ "BASIC is easy,\n",
+ "FORTAN is fun,\n",
+ "Pascal may be structured,\n",
+ "But C's the one!\n",
+ "\n",
+ "You entered:\n",
+ "BASIC is easy,\n",
+ "FORTAN is fun,\n",
+ "Pascal may be structured,\n",
+ "But C's the one!\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 14-6, Page Number: 426"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Using several files\n",
+ "\n",
+ "# Open the source file for reading\n",
+ "\n",
+ "first_file_pointer = open(\"first_file\",\"r\")\n",
+ "\n",
+ "# Open the destination file for writing\n",
+ "\n",
+ "second_file_pointer = open(\"second_file\",\"w\")\n",
+ "\n",
+ "# Transfer characters from source to destination file\n",
+ "\n",
+ "c = first_file_pointer.read()\n",
+ "second_file_pointer.write(c)\n",
+ "\n",
+ "# Close both files\n",
+ "\n",
+ "first_file_pointer.close()\n",
+ "second_file_pointer.close()\n",
+ "\n",
+ "# Open second file for displaying contentes\n",
+ "\n",
+ "second_file_pointer = open(\"second_file\",\"r\")\n",
+ "print(\"'second_file' contains:\")\n",
+ "print second_file_pointer.read()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "'second_file' contains:\n",
+ "BASIC is easy,\n",
+ "FORTAN is fun,\n",
+ "Pascal may be structured,\n",
+ "But C's the one!\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 14-7, Page Number : 428"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Append mode\n",
+ "\n",
+ "# Open the first file for writing\n",
+ "\n",
+ "first_file_pointer = open(\"first_file\",\"w\")\n",
+ "\n",
+ "# Open the second file for appending\n",
+ "\n",
+ "second_file_pointer = open(\"second_file\",\"a\")\n",
+ "\n",
+ "# Writing to both files\n",
+ "\n",
+ "first_file_pointer.write(\"\\n--Anonymous\\n\")\n",
+ "second_file_pointer.write(\"\\n--Anonymous\\n\")\n",
+ "\n",
+ "# Close both files\n",
+ "\n",
+ "first_file_pointer.close()\n",
+ "second_file_pointer.close()\n",
+ "\n",
+ "# Display the contents of first file\n",
+ "first_file_pointer = open(\"first_file\",\"r\")\n",
+ "print(\"After write, 'first_file contains:\")\n",
+ "print first_file_pointer.read()\n",
+ "\n",
+ "# Display the contents of second file\n",
+ "second_file_pointer = open(\"second_file\",\"r\")\n",
+ "print(\"After append, 'second_file' contains:\")\n",
+ "print second_file_pointer.read()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "After write, 'first_file contains:\n",
+ "\n",
+ "--Anonymous\n",
+ "\n",
+ "After append, 'second_file' contains:\n",
+ "BASIC is easy,\n",
+ "FORTAN is fun,\n",
+ "Pascal may be structured,\n",
+ "But C's the one!\n",
+ "--Anonymous\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 14-8, Page Number: 431"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# The seek function\n",
+ "\n",
+ "# Open file for reading\n",
+ "try :\n",
+ "\tfile_pointer = open(\"second_file\",\"r\")\n",
+ "except :\n",
+ "\tprint \"'second_file' cannot be opened\"\n",
+ "\texit ()\n",
+ "\n",
+ "# Skip the first 6 characters\n",
+ "\n",
+ "file_pointer.seek(6,0)\n",
+ "print file_pointer.read()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "is easy,\n",
+ "FORTAN is fun,\n",
+ "Pascal may be structured,\n",
+ "But C's the one!\n",
+ "--Anonymous\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 14-9, Page Number: 433"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# The 'w+' mode and seek to rewind a file\n",
+ "\n",
+ "# Open a file for read/write\n",
+ "file_pointer = open(\"first_file\",\"w+\")\n",
+ "\n",
+ "print \"Enter text:\"\n",
+ "text = \"BASIC is easy,\\nFORTAN is fun,\\nPascal may be structured,\\nBut C's the one!\"\n",
+ "print text\n",
+ "file_pointer.write(text)\n",
+ "\n",
+ "file_pointer.seek(0,0)\n",
+ "\n",
+ "# Display the file\n",
+ "print \"\\nYou entered:\"\n",
+ "print file_pointer.read()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter text:\n",
+ "BASIC is easy,\n",
+ "FORTAN is fun,\n",
+ "Pascal may be structured,\n",
+ "But C's the one!\n",
+ "\n",
+ "You entered:\n",
+ "BASIC is easy,\n",
+ "FORTAN is fun,\n",
+ "Pascal may be structured,\n",
+ "But C's the one!\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file