def PtrSeek(fptr):
offset1 = PtrTell(fptr)
DataRead(fptr)
offset2 = PtrTell(fptr)
DataRead(fptr)
offset3 = PtrTell(fptr)
DataRead(fptr)
print "\nRe-read the haiku:\n"
fptr.seek(offset3)
DataRead(fptr)
fptr.seek(offset2)
DataRead(fptr)
fptr.seek(offset1)
DataRead(fptr)
def PtrTell(fprt):
reval = fptr.tell()
print "The fptr is at: ",reval,"\n"
return reval
def DataRead(fptr):
buff = range(80)
buff = fptr.readline()
print buff
def ErrorMsg(str):
print "Cannot open",str,"\n"
return FAIL
MAX_LEN = 80
try:
filename = "haiku.txt"
reval = "SUCCESS"
fptr = open(filename,"r")
if(not isinstance(fptr,file)):
reval = ErrorMsg(filename)
else:
PtrSeek(fptr)
fptr.close()
except IOError:
print "\nFile can not be opened\n"
"""
haiku.txt (Read file)
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)
"""
import sys
MAX_NUM = 3
def DataWrite(fout):
buff = [123.45,567.89,100.11]
print "The size of buff:",sys.getsizeof(buff),"-byte\n”, sizeof(buff)"
for i in range(MAX_NUM):
print buff[i],"\n"
fout.write(str(buff[i]) + "\n")
def DataRead(fin):
print "\nRead back from the binary file:\n"
for i in range(MAX_NUM):
x = fin.readline()
print x,"\n"
def ErrorMsg(str):
print "Cannot open",str,"\n"
return FAIL
try:
filename = "double.bin"
reval = "SUCCESS"
fptr = open(filename,"wb")
if(not isinstance(fptr,file)):
reval = ErrorMsg(filename)
else:
DataWrite(fptr)
# rewind(fptr) No Such Function In Python
fptr.close()
fptr = open(filename,"r")
DataRead(fptr)
fptr.close()
except IOError:
print "\nFile can not be opened\n"
"""
double.bin (Writing Binary File)
123.45
567.89
100.11
"""
import sys
MAX_NUM = 3
STR_LEN = 23
def DataWrite(fout):
cities = range(MAX_NUM)
cities = ["St.Louis->Houston:","Houston->Dallas:","Dallas->Philadelphia:"]
miles = range(MAX_NUM)
miles = [845,243,1459]
print "The data written:\n"
for i in range(MAX_NUM):
print cities[i],miles[i],"\n"
fout.writelines(str(cities[i]) + str(miles[i]) + "\n")
def DataRead(fin):
print "\nThe data read:\n"
for i in range(MAX_NUM):
cities = fin.read()
miles = fin.read()
print cities,miles
def ErrorMsg(str):
print "Cannot open",str,"\n"
return FAIL
try:
filename = "strnum.mix"
reval = "SUCCESS"
fptr = open(filename,"w+")
if(not isinstance(fptr,file)):
reval = ErrorMsg(filename)
else:
DataWrite(fptr)
# rewind(fptr) No Such Function In Python
fptr.close()
fptr = open(filename,"r")
DataRead(fptr)
fptr.close()
except IOError:
print "\nFile can not be opened\n"
"""
strnum.mix (Writing and Reading File)
St.Louis->Houston:845
Houston->Dallas:243
Dallas->Philadelphia:1459
"""
STR_NUM = 4
def ErrorMsg(str1):
print "Cannot open",str,"\n"
return FAIL
def StrPrint(str1):
for i in range(STR_NUM):
print str1[i],"\n"
stdout.writelines(str1[i] + "\n")
try:
str1 = range(STR_NUM)
str1 = ["Be bent, and you will remain straight.","Be vacant, and you will remain full.","Be worn, and you will remain new.","--- by Lao Tzu"]
filename = "LaoTzu.txt"
reval = "SUCCESS"
stdout = open(filename,"w+")
StrPrint(str1)
stdout.close()
stdout = open(filename,"w+")
if(not isinstance(stdout,file)):
reval = ErrorMsg(filename)
else:
StrPrint(str1)
stdout.close()
except IOError:
print "\nFile can not be opened\n"
"""
Output File : LaoTzu.txt
Be bent, and you will remain straight.
Be vacant, and you will remain full.
Be worn, and you will remain new.
--- by Lao Tzu
"""