python 核心编程第二版 9-9

来源:互联网 发布:中国海关进出口数据库 编辑:程序博客网 时间:2024/05/29 15:08
原题:
进入python标准库所在的目录。检查每个.py 文件看是否有doc,如果有,对其格式进行适当的整理归类。你的程序执行完毕后,应该生成一个漂亮的清单。里边列出哪些模块有文档字符串,以及文档字符串的内容,清单最后会附上哪些没有文档的字符串模块的名字


研究一下午,还是逻辑思维有漏洞。下面代码实现主要功能即可以将所有的文档的doc输出至文件并且能够找出有文档的py文件。

import osfile_list = []def getlist(path):    global file_list    path = os.path.abspath(path)    for i in os.listdir(path):        if os.path.isdir(path+'\\'+i):            getlist(path+'\\'+i)        elif os.path.splitext(i)[1] == '.py':            file_list.append(path+'\\'+i)def checkdoc():    have_doc = []    file1 = open('t.txt', 'w')    global file_list    for file_tmp in file_list:        str1 = ''        print 'curr file: %s and per: %d ' % (file_tmp, file_list.index(file_tmp))        for line in open(file_tmp):            if '"""' in line and str1 == '':                str1 = 'file %s doc \n' % file_tmp                have_doc.append(file_tmp)                str1 += line            elif file_tmp in have_doc:                if '"""' in line:                    str1 += line                    str1 += '\n----------------------------------------\n'                    file1.write(str1)                    break                else:                    str1 += line    file1.close()    print have_docif __name__ == '__main__':    getlist('c:\\Python27\\Lib')    checkdoc()


0 0
原创粉丝点击