Hour 21 : Reading and Writing with Files

Example 21.1, Page No 359

In [3]:
try:
    filename = "haiku.txt"
    reval = "SUCCESS"
    fptr = open(filename,"r")
    if(not isinstance(fptr,file)):
        print "\nCan not open ",filename,"\n"
        reval = "FAIL"
    else:
        print "The value of fptr: ",fptr,"\n"
        print "Ready to close the file."
        fptr.close()
except IOError:
    print "\nFile can not be opened\n"
The value of fptr:  <open file 'haiku.txt', mode 'r' at 0x000000000394DB70> 

Ready to close the file.

Example 21.2, Page No 361

In [1]:
def CharReadWrite(fin,fout):
    while(1):
        c = fin.readline()
        if c == "":
            break
        else:
            print c
            fout.writelines(c)
try:
    filename1 = "outhaiku.txt"
    filename2 = "haiku.txt"
    reval = "SUCCESS"
    fptr1 = open(filename1,"w")
    fptr2 = open(filename2,"r")
    if(not isinstance(fptr1,file)):
        print "\nCan not open ",filename1,"\n"
        reval = "FAIL"
    elif(not isinstance(fptr1,file)):
        print "\nCan not open ",filename2,"\n"
        reval = "FAIL"
    else:
        CharReadWrite(fptr2,fptr1)
        fptr1.close()
        fptr2.close()
except IOError:
    print "\nFile can not be opened\n"
Leading me along

my shadow goes back home

from looking at the moon.

--- Sodo

(1641-1716)

A storm wind blows

out from among the grasses

the full moon grows.

--- Chora

(1729-1781)

Example 21.3, Page No 364

In [2]:
def LineReadWrite(fin,fout):
    while(1):
        c = fin.readline()
        if c == "":
            break
        else:
            print c
            fout.writelines(c)
try:
    filename1 = "outhaiku.txt"
    filename2 = "haiku.txt"
    reval = "SUCCESS"
    fptr1 = open(filename1,"w")
    fptr2 = open(filename2,"r")
    if(not isinstance(fptr1,file)):
        print "\nCan not open ",filename1,"\n"
        reval = "FAIL"
    elif(not isinstance(fptr1,file)):
        print "\nCan not open ",filename2,"\n"
        reval = "FAIL"
    else:
        LineReadWrite(fptr2,fptr1)
        fptr1.close()
        fptr2.close()
except IOError:
    print "\nFile can not be opened\n"
Leading me along

my shadow goes back home

from looking at the moon.

--- Sodo

(1641-1716)

A storm wind blows

out from among the grasses

the full moon grows.

--- Chora

(1729-1781)

Example 21.4, Page No 368

In [3]:
def BlockReadWrite(fin,fout):
    while(1):
        c = fin.readline()
        if c == "":
            break
        else:
            print c
            fout.writelines(c)
def ErrorMsg(str1):
    print "Cannot open: ",str1,"\n"
    return FAIL
try:
    filename1 = "outhaiku.txt"
    filename2 = "haiku.txt"
    reval = "SUCCESS"
    fptr1 = open(filename1,"w")
    fptr2 = open(filename2,"r")
    if(not isinstance(fptr1,file)):
        print "\nCan not open ",filename1,"\n"
        reval = ErrorMsg(filename1)
    elif(not isinstance(fptr1,file)):
        print "\nCan not open ",filename2,"\n"
        reval = ErrorMsg(filename2)
    else:
        BlockReadWrite(fptr2,fptr1)
        fptr1.close()
        fptr2.close()
except IOError:
    print "\nFile can not be opened\n"
Leading me along

my shadow goes back home

from looking at the moon.

--- Sodo

(1641-1716)

A storm wind blows

out from among the grasses

the full moon grows.

--- Chora

(1729-1781)