summaryrefslogtreecommitdiff
path: root/Practical_C_Programming/Chapter_14_1.ipynb
blob: b2876b441daa4ccf6769e5c9c2e7f5f2b92c9c28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{
 "metadata": {
  "name": "Chapter 14"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "Chapter 14: File input/output"
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "Example 14.1, Page number: 253"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# Example 14.1.py\n# To count the number of characters in the file 'input.txt'\n\n\n# Variable declaration\nimport os\ncount = 0\nin_file = open ('input.txt', 'r')\n \n# Calculation and result\nif not os.path.exists ('input.txt') :\n   print ('Cannot open input.txt\\n')\n\nwhile (1) :\n   ch = in_file.read(1)\n   if not ch :\n      break\n   count += 1\n\nprint ('Number of characters in input.txt is %d\\n' % count)\n\nin_file.close()",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "IOError",
       "evalue": "[Errno 2] No such file or directory: 'input.txt'",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIOError\u001b[0m                                   Traceback (most recent call last)",
        "\u001b[1;32m<ipython-input-1-038814efe2a7>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[0mcount\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0min_file\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'input.txt'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'r'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;31m# Calculation and result\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'input.txt'"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "Example 14.3, Page number: 257"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# Example 14.3.py\n# To check whether a file exists or not\n\n\n# Variable declaration\nimport sys\nimport os\nin_file = open ('input.txt', 'r')\n \n# Calculation and result\nif not os.path.exists (name) :\n   print ('Could not open file\\n')\n   sys.exit(1)\nprint ('File found\\n')\n\nin_file.close()",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "IOError",
       "evalue": "[Errno 2] No such file or directory: 'input.txt'",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mIOError\u001b[0m                                   Traceback (most recent call last)",
        "\u001b[1;32m<ipython-input-2-370dc09ecbc3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0min_file\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'input.txt'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'r'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;31m# Calculation and result\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'input.txt'"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "Example 14.4, Page number: 260"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# Example 14.4.py\n# To write to a file\n\n\n# Variable declaration\nimport sys\nimport os\nout_file = open ('test.out', 'w')\n \n# Calculation and result\nif not os.path.exists ('test.out') :\n   print ('Cannot open output file\\n')\n   sys.exit(1)\n\nfor cur_char in range (0, 128) :\n   out_file.write(str (cur_char))\n   out_file.write('\\n')\n\nout_file.close()",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "Example 14.5, Page number: 267"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# Example 14.5.py\n# To copy the contents of a file to another file\n\n\n# Variable declaration\ntxt_file = open ('input.txt')\n\nsource_content = txt_file.read()\n\ntarget = open ('output.txt', 'w')\n\n# Calculation and result\ntarget.write(source_content)\nprint ('Content copied')\n\ntarget.close()",
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}