"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Write data to text file and read it\n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a')\n",
"\n",
"#Write data to file if successfully created the file\n",
"if fp == 0:\n",
" sys.stdout.write(\"Cannot open file\")\n",
"else:\n",
" sys.stdout.write(\"Write data & to stop press '.'\")\n",
" c = ''\n",
" while c != '.':\n",
" c = raw_input(\"Write data & to stop press '.'\")\n",
" fp.write(c)\n",
" fp.close()\n",
"\n",
" #Read and display the contents of file\n",
" sys.stdout.write(\"\\nContents read : \")\n",
" fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n",
" f = fp.readlines()\n",
" for f1 in f:\n",
" print f1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Write data & to stop press '.'"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Write data & to stop press '.'ABCDEFGHIJK\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Write data & to stop press '.'.\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Contents read : ABCDEFGHIJK.\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.2, Page number: 455
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the contents of the file before and after appending.\n",
"\n",
"import sys\n",
"\n",
"#Open file for read data\n",
"fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n",
"\n",
"#Read and display the contents of file\n",
"sys.stdout.write(\"\\nContents of file before appending :\\n\")\n",
"fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n",
"f = fp.readlines()\n",
"for f1 in f:\n",
" print f1\n",
"\n",
"#Open file for appending\n",
"fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a')\n",
"\n",
"if f == 0:\n",
" sys.stdout.write(\"File can not appended\")\n",
"else:\n",
" c = ''\n",
" #fp.write('\\n')\n",
" while c != '.':\n",
" c = raw_input(\"Enter string to append\")\n",
" fp.write(c)\n",
" \n",
" fp.close()\n",
" \n",
" #Read and display the contents of file\n",
" sys.stdout.write(\"\\nContents of file After appending\\n\")\n",
" fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n",
" f = fp.readlines()\n",
" for f1 in f:\n",
" print f1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Contents of file before appending :\n",
"String is terminated with '\\0'.\n",
"\n",
"Contents of file After appending\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"String is terminated with '\\0'.This character is called as NULL character.\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.3, Page number: 457
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Writing and reading of a file.\n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','w+')\n",
"\n",
"#Write data to file if successfully created the file\n",
"if fp == 0:\n",
" sys.stdout.write(\"Cannot open file\")\n",
"else:\n",
" sys.stdout.write(\"Write data & to stop press '.'\")\n",
" c = ''\n",
" while c != '.':\n",
" c = raw_input(\"Write data & to stop press '.'\")\n",
" fp.write(c)\n",
" fp.close()\n",
"\n",
" #Read and display the contents of file\n",
" sys.stdout.write(\"\\nContents read : \")\n",
" fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n",
" f = fp.readlines()\n",
" for f1 in f:\n",
" print f1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Write data & to stop press '.'\n",
"Contents read : "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"ABCDEFGHIJK.\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.4, Page number: 458
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Open a file in append mode and add new records in it.\n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a+')\n",
"\n",
"#Write data to file if successfully created the file\n",
"if fp == 0:\n",
" sys.stdout.write(\"Cannot open file\")\n",
"else:\n",
" sys.stdout.write(\"Write data & to stop press '.'\")\n",
" c = ''\n",
" while c != '.':\n",
" c = raw_input(\"Write data & to stop press '.'\")\n",
" fp.write(c)\n",
" fp.close()\n",
"\n",
" #Read and display the contents of file\n",
" sys.stdout.write(\"\\nContents read : \")\n",
" fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a+')\n",
" f = fp.readlines()\n",
" for f1 in f:\n",
" print f1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Write data & to stop press '.'\n",
"Contents read : "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"This is append and read mode.\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.5, Page number: 459
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Open a file in read/write mode in it\n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r+')\n",
"\n",
"#Write data to file if successfully created the file\n",
"if fp == 0:\n",
" sys.stdout.write(\"Cannot open file\")\n",
"else:\n",
" #Read and display the contents of file\n",
" sys.stdout.write(\"\\nContents read : \")\n",
" f = fp.readlines()\n",
" for f1 in f:\n",
" print f1\n",
" \n",
" sys.stdout.write(\"Write data & to stop press '.'\")\n",
" c = ''\n",
" while c != '.':\n",
" c = raw_input(\"Write data & to stop press '.'\")\n",
" fp.write(c)\n",
" fp.close()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Contents read : Help me.\n",
"Write data & to stop press '.'"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.6, Page number: 460
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Open a file for read/write operation in binary mode\n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','wb')\n",
"\n",
"#Write data to file if successfully created the file\n",
"if fp == 0:\n",
" sys.stdout.write(\"Cannot open file\")\n",
"else:\n",
" sys.stdout.write(\"Write data & to stop press '.'\")\n",
" c = ''\n",
" while c != '.':\n",
" c = raw_input(\"Write data & to stop press '.'\")\n",
" fp.write(c)\n",
" fp.close()\n",
"\n",
" #Read and display the contents of file\n",
" sys.stdout.write(\"\\nContents read : \")\n",
" fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','rb')\n",
" f = fp.readlines()\n",
" for f1 in f:\n",
" print f1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Write data & to stop press '.'\n",
"Contents read : "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"ABCDEFGHIJK.\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.7, Page number: 462
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Open a text file and write some text \n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','w')\n",
"\n",
"#Read the string\n",
"text = raw_input(\"Enter Text Here : \")\n",
"\n",
"#Write to the file\n",
"#fp.write() is the equivalent function for fprintf() in python\n",
"fp.write(\"%s\"%(text))\n",
"\n",
"#To see the result, open the data.txt file"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read the contents of the file \n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"f = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\list.txt','r')\n",
"\n",
"if f == 0:\n",
" sys.stdout.write(\"Cannot open file\")\n",
"else:\n",
" for c in f:\n",
" sys.stdout.write(\"%s\"%(c))\n",
" #Python gives iterative statements there is no getc() fucntion"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"aman\n",
"akash\n",
"amit\n",
"ajay\n",
"ankit"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.10, Page number: 464
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Write some text into the file \n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\words.doc','w')\n",
"\n",
"#Write data to file\n",
"c = ''\n",
"while c != '*':\n",
" c = raw_input(\"Enter Few Words '*' to Exit\")\n",
" fp.write(c)\n",
"\n",
"fp.close()\n",
" #There is no putc() function in python\n",
" #Open the file to see the result"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.11, Page number: 465
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Count\n",
"\n",
"# Total number of statements\n",
"# Total number of included files\n",
"# Total number of blocks and brackets\n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fs = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\PRG2.C','r')\n",
"\n",
"#variable initialization\n",
"i = 0\n",
"c = 0\n",
"sb = 0\n",
"b = 0\n",
"\n",
"if fs == 0:\n",
" sys.stdout.write(\"File opening error\")\n",
"else:\n",
" for line in fs:\n",
" k = 0\n",
" while k < len(line):\n",
" if line[k] == ';':\n",
" c += 1\n",
" else:\n",
" if line[k] == '{':\n",
" sb += 1\n",
" else:\n",
" if line[k] == '(':\n",
" b += 1\n",
" else:\n",
" if line[k] == '#':\n",
" i += 1\n",
" k += 1\n",
" \n",
" #Result\n",
" sys.stdout.write(\"\\nSummary of 'C' Program\\n\")\n",
" sys.stdout.write(\"===========================\")\n",
" sys.stdout.write(\"\\nTotal Statments : %d\"%(c+i))\n",
" sys.stdout.write(\"\\nInclude Statements : %d\"%(i))\n",
" sys.stdout.write(\"\\nTotal Blocks {} : %d\"%(sb))\n",
" sys.stdout.write(\"\\nTotal Brackets () : %d\"%(b))\n",
" sys.stdout.write(\"\\n============================\")\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Summary of 'C' Program\n",
"===========================\n",
"Total Statments : 8\n",
"Include Statements : 2\n",
"Total Blocks {} : 2\n",
"Total Brackets () : 9\n",
"============================"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.12, Page number: 466
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Write text to a file \n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\lines.txt','w')\n",
"\n",
"if fp == 0:\n",
" sys.stdout.write(\"\")\n",
"else:\n",
" #Write data to file\n",
" c = ''\n",
" while c != '*':\n",
" c = raw_input(\"\")\n",
" fp.write(c)\n",
" fp.close()\n",
" #There is no fputc() function in python\n",
" #Open the file to see the result"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.13, Page number: 467
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read text from the given file \n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"\n",
"file1 = raw_input(\"Enter File Name : \")\n",
"fp = open(file1,'r')\n",
"\n",
"if fp == 0:\n",
" sys.stdout.write(\"File not found\\n\")\n",
"else:\n",
" for text in fp:\n",
" print text\n",
" #File pointer itself is iterable in python"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"#include \n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.14, Page number: 468
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Write a string into a file \n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"\n",
"file1 = raw_input(\"Enter the name of file : \")\n",
"fp = open(file1,'w')\n",
"\n",
"if fp == 0:\n",
" sys.stdout.write(\"File can not opened\\n\")\n",
"else:\n",
" text = raw_input(\"Enter Text Here : \")\n",
" fp.write(text)\n",
" \n",
" #Write() function is used to write character or string into the file in python\n",
" # there is no fputc() function in python\n",
"#open the file to see the result"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.15, Page number: 469
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Write integers in a file \n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open('num.txt','w')\n",
"\n",
"if fp == 0:\n",
" sys.stdout.write(\"File does not exist\\n\")\n",
"else:\n",
" v = ' '\n",
" while v != '0':\n",
" fp.write(v)\n",
" v = raw_input(\"Enter Numbers\")\n",
" \n",
" #open the file to see the result"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read the text after skipping n characters from beginning of the file\n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open(\"text.txt\",\"r\")\n",
"\n",
"sys.stdout.write(\"\\nContents of file\\n\")\n",
"ch = fp.read()\n",
"sys.stdout.write(\"%s\"%(ch))\n",
"\n",
"#Read n\n",
"n = int(raw_input(\"How many characters including spaces would you like to skip?\"))\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nInformation after %d bytes\\n\"%(n))\n",
"\n",
"ch = ch[n:]\n",
"sys.stdout.write(\"%s\"%(ch))\n",
"\n",
"fp.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Contents of file\n",
"THE C PROGRAMMING LANGUAGE INVENTED BY DENNIS RITCHIE"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"How many characters including spaces would you like to skip?18\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Information after 18 bytes\n",
"LANGUAGE INVENTED BY DENNIS RITCHIE"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.22, Page number: 477
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read last few characters of the file \n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open(\"text.txt\",\"r\")\n",
"\n",
"sys.stdout.write(\"\\nContents of file\\n\")\n",
"ch = fp.read()\n",
"sys.stdout.write(\"%s\"%(ch))\n",
"\n",
"#Read n\n",
"n = int(raw_input(\"How many characters including spaces would you like to skip?\"))\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nInformation after %d bytes\\n\"%(n))\n",
"\n",
"print ch[-n:]\n",
"\n",
"fp.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Contents of file\n",
"HELLO WORLD"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"How many characters including spaces would you like to skip?5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Information after 5 bytes\n",
"WORLD\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.23, Page number: 477
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display 'c' program files in current directory. \n",
"\n",
"import sys\n",
"\n",
"#Variable initialization\n",
"l = 0\n",
"c = 0\n",
"\n",
"#Open file\n",
"file1 = raw_input(\"Enter the file name : \")\n",
"fp = open(file1,'r')\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nContents of 'c' program file in capital case\")\n",
"sys.stdout.write(\"\\n============================================\\n\")\n",
"\n",
"ch = fp.readlines()\n",
"\n",
"for line in ch:\n",
" i = 0\n",
" #print line.upper()\n",
" while i < len(line):\n",
" if line[i] =='\\n':\n",
" l += 1\n",
" else:\n",
" c += 1\n",
" sys.stdout.write(\"%c\"%(line[i].upper()))\n",
" i += 1\n",
"\n",
"sys.stdout.write(\"\\nTotal Characters : %d\"%(c))\n",
"sys.stdout.write(\"\\nTotal Lines : %d\"%(l))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Contents of 'c' program file in capital case\n",
"============================================\n",
"MAIN()\n",
"{\n",
"PRINTF(\" HELLO WORLD\");\n",
"}\n",
"\n",
"Total Characters : 31\n",
"Total Lines : 4"
]
}
],
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.24, Page number: 479
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Detect the end of file \n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open(\"text.txt\",\"r\")\n",
"\n",
"#there is no feof() function in python\n",
"c = fp.tell()\n",
"sys.stdout.write(\"File pointer at the beginning of the file : %d\\n\"%(c))\n",
"\n",
"c = fp.read()\n",
"sys.stdout.write(\"%s\"%(c))\n",
"\n",
"c = fp.tell()\n",
"sys.stdout.write(\"\\nFile pointer at the end of file : %d\"%(c))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"File pointer at the beginning of the file : 0\n",
"TECHNOCRATS LEAD THE WORLD \n",
"File pointer at the end of file : 32"
]
}
],
"prompt_number": 26
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Print the current position of the file pointer \n",
"\n",
"import sys\n",
"\n",
"#Open file\n",
"fp = open(\"text.txt\",\"r\")\n",
"\n",
"#Set the pointer\n",
"fp.seek(21)\n",
"\n",
"#Result\n",
"while True:\n",
" c = fp.read(1)\n",
" if not c:\n",
" break\n",
" sys.stdout.write(\"%c\\t%d\\n\"%(c,fp.tell()))\n",
"#There is no endof file in python"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"W\t22\n",
"O\t23\n",
"R\t24\n",
"L\t25\n",
"D\t26\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.29, Page number: 483
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Detect beginning of file\n",
"\n",
"import sys\n",
"\n",
"#open file\n",
"fp = open(\"text.txt\",\"r\")\n",
"\n",
"fp.seek(12)\n",
"sys.stdout.write(\"Pointer is at %d\\n\"%(fp.tell()))\n",
"sys.stdout.write(\"Before rewind() : \")\n",
"\n",
"#Result\n",
"while True:\n",
" c = fp.read(1)\n",
" if not c:\n",
" break\n",
" sys.stdout.write(\"%c\"%(c))\n",
" \n",
"sys.stdout.write(\"\\nAfter rewind() : \")\n",
"fp.seek(0)\n",
"#There is no rewind function in python\n",
"\n",
"while True:\n",
" c = fp.read(1)\n",
" if not c:\n",
" break\n",
" sys.stdout.write(\"%c\"%(c))\n",
" \n",
"fp.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Pointer is at 12\n",
"Before rewind() : LEAD THE WORLD\n",
"After rewind() : TECHNOCRATS LEAD THE WORLD"
]
}
],
"prompt_number": 25
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.30, Page number: 484
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Delete the given file \n",
"\n",
"import sys\n",
"import os\n",
"\n",
"#Read file name\n",
"file1 = raw_input(\"Enter The File Name : \")\n",
"\n",
"#There is no remove or unlink file function in python.\n",
"#A file can be deleted using remove function in the os module.\n",
"\n",
"try:\n",
" os.remove(file1)\n",
" sys.stdout.write(\"\\nFile (%s) has been deleted!\"%(file1))\n",
"except:\n",
" sys.stdout.write(\"\\nFile does not exist\")\n",
"\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"File (TEXT.TXT) has been deleted!"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.31, Page number: 485
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Change the name of the file\n",
"\n",
"import os\n",
"import sys\n",
"\n",
"old = raw_input(\"Old File Name : \")\n",
"\n",
"new = raw_input(\"New File Name : \")\n",
"\n",
"os.rename(old, new)\n",
"\n",
"#Check the directory to see the result\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.32, Page number: 486
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Copy the contents of one file to another file\n",
"\n",
"import sys\n",
"\n",
"fs = open(\"a.txt\",\"r\")\n",
"ft = open(\"b.txt\",\"w\")\n",
"c = 0\n",
"\n",
"if fs == 0:\n",
" sys.stdout.write(\"\\nSource file opening error.\")\n",
"else:\n",
" if ft == 0:\n",
" sys.stdout.write(\"\\nTarget file opening error.\")\n",
" else:\n",
" while True:\n",
" ch = fs.read(1)\n",
" if not ch:\n",
" break\n",
" ft.write(\"%c\"%(ch))\n",
" c += 1\n",
" sys.stdout.write(\"\\n%d Bytes copied from 'a.txt' to 'b.txt'.\"%(c))\n",
" c = 0\n",
" #there is no fcloseall() function in python\n",
" fs.close()\n",
" c += 1\n",
" ft.close()\n",
" c += 1\n",
" sys.stdout.write(\"\\n%d files closed.\"%(c))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"45 Bytes copied from 'a.txt' to 'b.txt'.\n",
"2 files closed."
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.33, Page number: 487
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read the contents of three files and find the largest file\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"y = 0\n",
"k = 0\n",
"t = 0\n",
"name = [\"1.txt\",\"2.txt\",\"3.txt\"]\n",
"f = [0 for i in range(0,3)]\n",
"x = [0 for i in range(0,3)]\n",
"\n",
"#Open all the files\n",
"for l in range(0,3):\n",
" fp = open(name[l],\"r\")\n",
" f[l] = fp\n",
" if fp == 0:\n",
" sys.stdout.write(\"\\n%s file not found.\"%(name[l]))\n",
" break\n",
" \n",
"#Read contents of all files\n",
"for l in range(0,3):\n",
" while True:\n",
" c1 = f[l].read(1)\n",
" if not c1:\n",
" break\n",
" x[l] = y\n",
" y += 1\n",
" y = 0\n",
"\n",
"#close the files\n",
"for l in range(0,3):\n",
" f[l].close()\n",
"\n",
"#Print size of all files\n",
"for l in range(0,2+1):\n",
" sys.stdout.write(\"File : %s Bytes : %d\\n\"%(name[l],x[l]))\n",
" t = t + x[l]\n",
" \n",
"#Find largest\n",
"for l in range(t,1,-1):\n",
" for k in range(0,3):\n",
" if l == x[k]:\n",
" sys.stdout.write(\"\\n%s are the largest file.\"%(name[k]))\n",
" break\n",
" if l == x[k]:\n",
" break"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"File : 1.txt Bytes : 16\n",
"File : 2.txt Bytes : 20\n",
"File : 3.txt Bytes : 25\n",
"\n",
"3.txt are the largest file."
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.34, Page number: 488
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Copy 100 characters from a file to an array\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"SIZE = 100\n",
"s = 0\n",
"x = 0\n",
"ch = ['0' for i in range(0,100)]\n",
"\n",
"#Open the files\n",
"f = open(\"poem.txt\",\"r\")\n",
"f2 = open(\"alpha.txt\",\"w\")\n",
"\n",
"if f ==0 or f2 == 0:\n",
" sys.stdout.write(\"?\")\n",
"else:\n",
" while True:\n",
" c = f.read(1)\n",
" x += 1\n",
" if not c:\n",
" break\n",
" else:\n",
" if x == 99:\n",
" break\n",
" else:\n",
" ch[s] = c\n",
" s += 1\n",
"\n",
"for s in range(0,100):\n",
" f2.write(\"%c\"%(ch[s]))\n",
"\n",
"sys.stdout.write(\"Process Completed : Error 0\")\n",
"f.close()\n",
"f2.close()\n",
"\n",
"#There is no perror() function in python"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Process Completed : Error 0"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.35, Page number: 491
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Low level disk I/O operations.\n",
"\n",
"import sys\n",
"\n",
"\n",
"#Variable Initialization\n",
"file1 = raw_input(\"Enter a file name : \")\n",
"\n",
"#Open file\n",
"s = open(file1,\"w\")\n",
"\n",
"#Result\n",
"if s == -1:\n",
" sys.stdout.write(\"File does not exits\")\n",
"else:\n",
" buff = raw_input(\"Enter text below:\")\n",
" s.write(\"%s\"%(buff))\n",
" s.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a file name : TEXT\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter text below:PROGRAMMING IN C\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.36, Page number: 492
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read text from a specified file \n",
"\n",
"import sys\n",
"\n",
"#Variable initialization\n",
"file1 = raw_input(\"Enter a file name \")\n",
"\n",
"#open file\n",
"s = open(file1,\"r\")\n",
"\n",
"#Result\n",
"if s == -1:\n",
" sys.stdout.write(\"File does not exists\")\n",
"else:\n",
" while True:\n",
" ch = s.read(1)\n",
" if not ch:\n",
" break\n",
" sys.stdout.write(\"%c\"%(ch))\n",
" \n",
"s.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a file name TEXT\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"PROGRAMMING IN C"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display number of arguments and their names\n",
"\n",
"import sys\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nTotal number of arguments are %d\\n\"%(len(sys.argv)))\n",
"\n",
"print str(sys.argv)\n",
"\n",
"#Command line arguments can be given in python by the command\n",
"\n",
"# python pgmname.py arg1 arg2 arg3\n",
"\n",
"#This is not possible in ipython notebook"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.39, Page number: 495
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read any file from command prompt\n",
"\n",
"import sys\n",
"\n",
"#open file\n",
"fp = open(sys.argv[1],\"r\")\n",
"\n",
"#Result\n",
"if fp == 0:\n",
" sys.stdout.write(\"Can not open file\")\n",
"else:\n",
" while True:\n",
" ch = fp.read(1)\n",
" if not ch:\n",
" break\n",
" sys.stdout.write(\"%c\"%(ch))\n",
" \n",
"fp.close()\n",
"\n",
"#This program can be run in python as\n",
"\n",
"# python pgmname.py filename"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.40, Page number: 495
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Command line argument to perform the task of DEL command\n",
"\n",
"import sys\n",
"\n",
"#Check number of arguments\n",
"if len(sys.argv) < 2:\n",
" sys.stdout.write(\"Insufficient Arguments\")\n",
"else:\n",
" fp = open(sys.argv[1],\"r\")\n",
" if fp == 0:\n",
" sys.stdout.write(\"File Not Found\")\n",
" fp.close()\n",
" os.remove(sys.argv[1])\n",
" sys.stdout.write(\"File has been deleted\")\n",
" \n",
"#This program can be deleted using\n",
"# python pgmname.py filename\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 14.41, Page number: 496
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Command line argument to perform the task of REN command\n",
"\n",
"import sys\n",
"\n",
"#Check number of arguments\n",
"if len(sys.srgv) < 3:\n",
" sys.stdout.write(\"Insufficient Arguments\")\n",
"else:\n",
" fp = open(sys.argv[1],\"r\")\n",
" if fp == 0:\n",
" sys.stdout.write(\"File Not Found\")\n",
" else:\n",
" sp = open(sys.argv[2],\"r\")\n",
" if sp == 0:\n",
" fp.close()\n",
" sp.close()\n",
" #Rename file\n",
" os.rename(sys.argv[1],sys.argv[2])\n",
" else:\n",
" sys.stdout.write(\"Duplicate file name or file is in use\")\n",
" \n",
"#This program can be executed as\n",
"\n",
"# python pgmname.py oldfilename newfilename\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"