9-1
def leachbetter():
f = open('yan.txt', 'w')
f.write('my file the first line/n')
f.write('# the leach/n')
f.write('the last line/n')
f = open('yan.txt')
for i in f:
if i[0] != '#':
print i,
f.close()
def leachbetter2():
f = open('yan.txt', 'w')
f.write('my file the first line\n')
f.write('# the leach\n')
f.write('the last line\n')
f.write('a test # line\n')
f = open('yan.txt')
for i in f:
if '#' in i:
num = i.index('#')
print i[:num],
else:
print i,
f.close()
leachbetter2()
9-2
def call(N, F):
f = open(F)
for i in range(N):
print f.readline(),
f.close()
call(2, 'yan.txt')
9-3
def linenum(F):
f = open(F)
n = 0
for i in f:
n += 1
f.close()
return n
print linenum('yan.txt')
def linenum2(F):
f = open(F)
num = len(f.readlines())
f.close()
return num
print linenum2('yan.txt')
9-4
def read25lines(a_file):
f = open(a_file)
go_on = True
while go_on:
raw_input('Enter any key to continue: ')
for i in range(25):
pr = f.readline()
if pr == '':
go_on = False
break
print pr,
f.close()
read25lines('yan.txt')
9-6
# two files
fstfile = open('yan.txt')
secfile = open('yu.txt')
# lists of lines of two files
list_fstfileline = fstfile.readlines()
list_secfileline = secfile.readlines()
# close the file
fstfile.close()
secfile.close()
def ziptwofiles(line1='', line2='', line=None, byte=None):
# zip the lines of each file
if line:
return zip(list_fstfileline, list_secfileline),\
len(list_fstfileline) == len(list_secfileline)
# zip the bytes of each line
if byte:
list_fstfilelinebyte = [i for i in line1]
list_secfilelinebyte = [i for i in line2]
return zip(list_fstfilelinebyte, list_secfilelinebyte),\
len(list_fstfilelinebyte) == len(list_secfilelinebyte)
def filecompare():
# zip of the lines
zipline = ziptwofiles(line=True)[0]
# count the line
line = 1
# compare the lines
for twolines in zipline:
# count the byte
byte = 1
# current lines are different
if twolines[0] != twolines[1]:
# zip of the bytes
zipbyte = ziptwofiles(twolines[0], twolines[1], byte=True)[0]
# compare the bytes
for twobytes in zipbyte:
# current bytes are different
if twobytes[0] != twobytes[1]:
return line, byte
# current bytes are the same
else:
byte += 1
# zip bytes are the same
else:
if not ziptwofiles(byte=True)[1]:
return line, byte
# current lines are the same
else:
line += 1
# zip lines are the same
else:
if ziptwofiles(line=True)[1]:
return 'All the same'
else:
return 'different in the length of two files'
print filecompare()
9-7
import ConfigParser
config = ConfigParser.ConfigParser()
# assume that we have two INI files xing.ini and xing2.ini
def analysis():
print config.read('xing.ini')
config.add_section('Section1')
config.set('Section1', 'python1', '1')
print config.sections()
with open('xing.ini', 'w') as f:
config.write(f)
def analysis2():
print config.read(['xing2.ini', 'xing.ini'])
print config.sections()
print config.items('Section1')
def analysis3():
print config.read('xing2.ini')
config.add_section('Section1')
config.set('Section1', 'python2', '2')
print config.sections()
with open('xing2.ini', 'w') as f:
config.write(f)
analysis2()