Chapter 10: Files

Example 1, page no. 372

In [7]:
fp = open("outfile1.txt", "w")
i = int(raw_input("Input an integer: "))
fp.write(str(i))
fp.close()
Input an integer: 22

Example 2, page no. 372

In [5]:
fp = open("outfile2.txt", "w")
fp.write("string written to file\n")
fp.write("c\n")
fp.close()

Example 3, page no. 372

In [8]:
fp = open("outfile1.txt", "r")
l = fp.readline()
print "The integer in outfile1.txt is ", l
The integer in outfile1.txt is  22

Example 10.1, page no. 374

In [18]:
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]
Enter student's name and mark: 
Use 'q' to stop entry
vivek
87
rajkumar
98
anand
67
babu
45
teja
90
q
Name	Mark
------------
vivek 	87

rajkumar 	98

anand 	67

babu 	45

teja 	90

Example 10.2, page no. 376

In [25]:
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)
Enter characters 
Use 'q' to stop entry
I
 
s
a
w
 
t
h
e
 
c
a
t
 
c
l
i
c
k
 
t
h
e
 
m
o
u
s
e
.
q
I saw the cat click the mouse. 
Number of characters in the file:  30

Example 10.3, page no. 378

In [1]:
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]
Enter the register number, name and mark: 
Use 'q' to stop entry
4005
babu
85
4007
rajkumar
98
4012
raju
77
4028
teja
93
4029
vivek
87
q
4005 	babu 		85

4007 	rajkumar 		98

4012 	raju 		77

4028 	teja 		93

4029 	vivek 		87

Example 10.4, page no. 381

In [19]:
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 file class.rec is of  70  bytes
Enter the record no. to read(0 to stop): 1
4005 babu 85

Enter the record no. to read(0 to stop): 2
4007 rajkumar 98

Enter the record no. to read(0 to stop): 3
4012 raju 77

Enter the record no. to read(0 to stop): 4
4028 teja 93

Enter the record no. to read(0 to stop): 0

Example 8, page no. 384

In [20]:
#The example will remain same as above

Example fwrite.c, page no. 390

In [23]:
#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()
In [24]:
#rest of the examples in the chapters deal with command line arguments which is not possible in IPython Notebook.