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
|
{
"metadata": {
"name": "chapter-13.ipynb"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Chapter 13: More Issues In Input/Output<h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Using argc and argv, Page number: 467<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"import sys\n",
"\n",
"if ( len(sys.argv) != 3 ):\n",
" print ( \"Improper number of arguments\" )\n",
" exit( )\n",
" \n",
"fs = open ( sys.argv[2], \"r\" ) #open file in read mode\n",
"if ( not fs):\n",
" print ( \"Cannot open source file\" )\n",
" exit( )\n",
" \n",
"ft = open ( sys.argv[2], \"w\" ) #open file in write mode\n",
"if ( not ft ):\n",
" print ( \"Cannot open target file\" )\n",
" fs.close()\n",
" exit( )\n",
" \n",
"while ( 1 ):\n",
" ch = fs.read(1)\n",
" if ( not ch ):\n",
" break\n",
" else:\n",
" ft.write(ch)\n",
"\n",
"#close files \n",
"fs.close()\n",
"ft.close()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Improper number of arguments\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
|