python文件读写、help保存为文件

写文件

fitting_results = []
# fit images
sumerror=0.0
f=open("D:/out.txt","w")
for j, i in enumerate(test_images):
    # obtain original landmarks
    gt_s = i.landmarks['PTS'].lms
    # generate perturbed landmarks
    s = fitter.perturb_shape(gt_s)
    # fit image
    fr = fitter.fit(i, s, gt_shape=gt_s) 
    # append fitting result to list
    fitting_results.append(fr)
    # print fitting info
    print>>f ,'Image: ', j
    print>>f,'Initial error: ', fr.initial_error
    print>>f,'Final error: ', fr.final_error
    sumerror=sumerror+fr.final_error/(len(test_images))
print>>f, 'mean error:',sumerror
print 'mean error:',sumerror

读文件

方法一:

f=open("SDMTrainer.rst")
line=f.readline()
 while line:
	print line
	line=f.readline()
方法二:

for line in open("SDMTrainer.rst"):
	print line

将help内容保存到文件中

import sys
import string
sys.stdout=open("D:/help.txt","w")
help(string)
sys.stdout.close()



你可能感兴趣的:(python文件读写、help保存为文件)