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
102
103
|
{
"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": {}
}
]
}
|