"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\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": [
"\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": [
"\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": [
"