9-15
def copyfile():
f1 = open('yanyuxing.txt')
f2 = open('yyx.txt', 'a')
f2.write(f1.read())
f1.close()
f2.close()
print 'done'
copyfile()
9-16
import os
def eightybytes(filename='yan.txt'):
f = open(filename)
lines = f.readlines()
result = []
for line in lines:
times = divmod(len(line.strip()), 80)
start = 0
stop = 80
for i in range(times[0] + 1):
result.append(line.strip()[start:stop] + os.linesep)
start += 80
stop += 80
if times[1] == 0:
break
f = open(filename, 'w')
f.writelines(result)
f.close()
print 'done'
eightybytes('yu.txt')
def eightybytes2(filename='yan.txt'):
f = open(filename)
lines = f.readlines()
result = []
for line in lines:
if len(line.strip()) <= 80:
result.append(line)
else:
theline = line.strip()
times = len(theline) / 80
length = len(theline)
start = 0
stop = 80
for i in range(times + 2):
while True:
if line[stop] in [' ', '.']:
result.append(line[start:stop + 1] + os.linesep)
break
else:
stop -= 1
if stop == length - 1:
break
start = stop + 1
stop = stop + 80 if stop + 80 < length else length - 1
f = open(filename, 'w')
f.writelines(result)
f.close()
print 'done'
eightybytes2()
9-17
import os
import os.path
filedic = {}
def creatext(f):
prompt = '''
(W)rite sth into the file
(Q)uit
Enter your choice: '''
while True:
choice = raw_input(prompt)
if choice.lower() == 'w':
context = raw_input('write a sentence: ')
f.write(context + os.linesep)
else:
f.seek(0)
break
def displaycontext():
choice = whichfile()
f = filedic[choice]
for line in f:
print line,
f.seek(0)
def editext():
choice = whichfile()
f = filedic[choice]
for i, j in enumerate(f):
print i, j
f.seek(0)
lines = f.readlines()
f.seek(0)
chooseindex = input('the index of the line you want to edit: ')
anewline = raw_input('change it to: ')
lines[chooseindex] = anewline + os.linesep
f.writelines(lines)
f.seek(0)
def savetext():
choice = whichfile()
f = filedic[choice]
lines = f.readlines()
path = raw_input('the path you want to save the file: ')
os.chdir(path)
filename = raw_input("the file's name: ")
sf = open(os.path.join(path, filename), 'w')
sf.writelines(lines)
f.close()
sf.close()
CMD = {'d': displaycontext, 'e': editext, 's': savetext}
def handletext():
prompt = '''
welcome to use your own text editor:
(C)reate a text
(D)isplay the context
(E)dit your text
(S)ave the file
(Q)uit
now enter your choice: '''
n = 0
while True:
while True:
try:
choice = raw_input(prompt).strip()[0].lower()
except (EOFError, KeyboardInterrupt):
choice = 'q'
print 'your choice: ' + choice
if choice not in 'cdesq':
print 'invalid option, try again.'
else:
break
if choice == 'q':
break
elif choice == 'c':
f = os.tmpfile()
n += 1
filedic['filename' + str(n)] = f
print "now this file's name is: " + 'filename' + str(n)
creatext(f)
else:
CMD[choice]()
def whichfile():
print 'now you have created these file: '
print filedic.keys()
choice = raw_input('choose the file you want: ')
return choice
handletext()
9-18
def searchbyte():
byte = chr(input('the value of an ASCII byte: '))
filename = raw_input('the file to search: ')
f = open(filename)
num = f.read().count(byte)
f.close()
print 'done'
return num
print searchbyte()
9-19
import random as ran
def binaryfile(chr_number, appear_number, file_length):
f = open('binaryfile.bin', 'wb')
printablebyte = range(0x20, 0x7e)
printablebyte.remove(chr_number)
writein = [chr(ran.choice(printablebyte)) for i in range(file_length)]
insertnumlist = range(0, file_length)
for i in range(appear_number):
insertnum = ran.choice(insertnumlist)
insertnumlist.remove(insertnum)
writein[insertnum] = chr(chr_number)
f.writelines(writein)
f.close()
print 'done'
binaryfile(0x40, 10, 20)