"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Program to check whether a file has been opened successfully before\n",
"trying to read or write to the file'''\n",
"\n",
"try :\n",
" fp = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" )#open file in read mode and check if file is opened successfully\n",
"except:\n",
" print \"cannot open file\" \n",
" exit()\n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"cannot open file\n"
]
}
],
"prompt_number": "*"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Counting Characters,Tabs,Spaces and Newlines , Page number: 423
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Program to count chars, spaces, tabs and newlines in a file.'''\n",
"\n",
"#Variable declaration\n",
"nol = 0\n",
"Not = 0\n",
"nob = 0\n",
"noc = 0\n",
"\n",
"fp = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #open file in read mode\n",
"while ( 1 ):\n",
" ch = fp.read(1)\n",
" if not ch :\n",
" break\n",
" noc = noc + 1 #number of chars\n",
" if ( ch == \" \" ):\n",
" nob = nob + 1 #number of spaces\n",
" if ( ch == \"\\n\" ):\n",
" nol = nol + 1 # number of newlines\n",
" if ( ch == \"\\t\" ):\n",
" Not = Not + 1 #number of tabs\n",
"\n",
"fp.close() #close file\n",
"\n",
"#Result\n",
"print \"Number of characters = \", noc \n",
"print \"Number of blanks = \", nob \n",
"print \"Number of tabs = \", Not \n",
"print \"Number of lines = \", nol \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Number of characters = 1495\n",
"Number of blanks = 254\n",
"Number of tabs = 0\n",
"Number of lines = 20\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
File Copy , Page number: 424
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Program to take the contents of a file and\n",
"copy them into another file, character by character.'''\n",
"\n",
"fs = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #open file in read mode\n",
"if(not fs):\n",
" print \"Cannot open source file\"\n",
" exit()\n",
"\n",
"ft = open ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"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.writelines(ch) #write into target file\n",
"\n",
"#closen files\n",
"fs.close()\n",
"ft.close()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Writing Strings to Files, Page number: 427
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Program to receive strings from keyboard and write them to file'''\n",
"\n",
"fp = open ( \"C:/Users/Akshatha M/Desktop/temp.txt\", \"w\" ) #open file in write mode\n",
"if(not fp):\n",
" print \"Cannot open target file\" \n",
" exit()\n",
"\n",
"print \"Enter a few lines of text:\" \n",
"#s=input(\"\") #Input strings from keyboard\n",
"s = \"File written\"\n",
"print s\n",
"while ( len(s) > 0 ):\n",
" fp.writelines(s) #write into file\n",
" fp.writelines(\"\\n\")\n",
" #s=input(\"\") #Input strings from keyboard\n",
" s=\"\"\n",
" \n",
"#close files\n",
"fp.close()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a few lines of text:\n",
"File written\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Reading Strings from Files , Page number: 429
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Program to reads strings from the file and displays them on screen.'''\n",
"\n",
"fp = open (\"C:/Users/Akshatha M/Desktop/carrot.txt\", \"r\" ) #open file in read mode\n",
"if ( not fp ):\n",
" print \"Cannot open file\" \n",
" exit( ) \n",
"\n",
"while ( 1 ) :#Read strings from file\n",
" s = fp.read(79)\n",
" if(s):\n",
" print s\n",
" else:\n",
" break\n",
" \n",
"\n",
"fp.close() #close file\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Method\n",
"Throughly was both beetroot and carrot to remove the mud on beetroot and\n",
" the hair on carrot\n",
"Peel off the carrot skin. do not peel the raddish skin to a\n",
"void possible loss of vitamins\n",
"Cut each beetroot and carrot in to 4 halves\n",
"Prea\n",
"ssure cook the beetroot and carrot. make 3 whistles so thet they become very sm\n",
"ooth\n",
"Drain and reserve the water after pressure cooking.\n",
"Mash the cooked pieces\n",
" and allow to cool\n",
"Cut onions into cubes and saute till golden brown on oil and\n",
" allow to cool\n",
"After cooling grind the mashed beetroot, carrot and sauted onion\n",
"s to paste in a mixy. add enough water to make paste not very fine.\n",
"Now strain \n",
"the grind paste.\n",
"Take some butter in a vessel add the garlic paste,gingerpaste \n",
"and green chilli paste and saute for some time till u get the specific garlic-b\n",
"eing-fried smell\n",
"Add moong sprouts and saute, cover with the lid and let cook f\n",
"or 5 mins\n",
"Now add the strain of beetroot,carrot and onions paste\n",
"Add 6 cups of \n",
"water to it and also the water reserved at the time of pressure cooking the bee\n",
"troot and carrot.\n",
"Add salt, cinnamon, cummin, coriander powder,lemon juice\n",
"Brin\n",
"g to a boil, let simmer for a few mins\n",
"Make a fine paste of cornflour and add i\n",
"t while stirring to avoid lumps\n",
"Til for somemore time so that the cornflour pas\n",
"te is cooked and mixed well\n",
"Take oil in a kadhai to fry the noodles.fry till go\n",
"lden brown and reserve on a tissue paper\n",
"Crush the noodles not too much\n",
"Serve s\n",
"teaming hot in soup bowls add some butter and top with the crispy noodles\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Writing Records to Files , Page number: 431
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Program to write records to a file using structure'''\n",
"\n",
"from collections import namedtuple\n",
"\n",
"#Variable declaration\n",
"another = 'Y'\n",
"\n",
"#Structure defintion\n",
"struct_emp = namedtuple(\"struct_emp\", \"name age bs\")\n",
"\n",
"\n",
"fp = open ( \"C:/Users/Akshatha M/Desktop/Employee.dat\", \"w\" ) #open file in write mode\n",
"if(not fp):\n",
" print \"Cannot open target file\"\n",
" exit()\n",
"\n",
"while ( another == 'Y' ):\n",
" print \"Enter name, age and basic salary: \" \n",
" #en,ea,ebs = input(\"\").split()\n",
" en =\"John\"\n",
" ea=\"34\"\n",
" ebs=\"25000\"\n",
" print en,ea,ebs\n",
" e = struct_emp(en,ea,ebs)\n",
" fp.writelines(e) #write into file\n",
" fp.writelines(\"\\n\")\n",
" #another = input( \"Add another record (Y/N): \" ) \n",
" print \"Add another record (Y/N): \"\n",
" another = 'N'\n",
" print another\n",
"#close file\n",
"fp.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter name, age and basic salary: \n",
"John 34 25000\n",
"Add another record (Y/N): \n",
"N\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"