fp = open("outfile1.txt", "w")
i = int(raw_input("Input an integer: "))
fp.write(str(i))
fp.close()
fp = open("outfile2.txt", "w")
fp.write("string written to file\n")
fp.write("c\n")
fp.close()
fp = open("outfile1.txt", "r")
l = fp.readline()
print "The integer in outfile1.txt is ", l
fp = open("sturec", "w")
print "Enter student's name and mark: "
print "Use 'q' to stop entry"
while True:
name = raw_input()
if name == 'q':
break
marks = raw_input()
fp.write(name+','+marks+'\n')
fp.close()
fp = open("sturec", "r")
print "Name\tMark"
print "------------"
lines = fp.readlines()
for line in lines:
line = line.split(',')
print line[0], '\t', line[1]
fp = open("str.txt", "w")
print "Enter characters "
print "Use 'q' to stop entry"
while True:
char = raw_input()
if char == 'q':
break
fp.write(char)
fp.close()
fp = open("str.txt", "r")
lines = fp.readlines()
count = 1
string = ''
for line in lines:
print line,
string += line
print "\nNumber of characters in the file: ", len(string)
fp = open("class.rec", "w")
print "Enter the register number, name and mark: "
print "Use 'q' to stop entry"
while True:
reg_no = raw_input()
if reg_no == 'q':
break
name = raw_input()
marks = raw_input()
fp.write(reg_no+','+name+','+marks+'\n')
fp.close()
fp = open("class.rec", "r")
lines = fp.readlines()
for line in lines:
line = line.split(',')
print line[0], '\t', line[1], '\t\t', line[2]
import os
import sys
fp = open("class.rec", "r")
fp.seek(0, os.SEEK_END)
size = fp.tell()
print "The file class.rec is of ", size, " bytes"
fp.seek(0)
lines = fp.readlines()
while True:
i = int(raw_input("Enter the record no. to read(0 to stop): "))
if i == 0:
break
fp.seek(0)
counter = 0
for line in lines:
counter += 1
if counter == i:
print line.split(",")[0], line.split(",")[1], line.split(",")[2]
break
#The example will remain same as above
#there is no concept of buffered write. We will do it normally
fp = open("testbuf.txt", "w")
fp.write("1. This is fwrite\n")
fp.write("2. this is write\n")
fp.close()
#rest of the examples in the chapters deal with command line arguments which is not possible in IPython Notebook.