9.9 Python Document String. Enter the directory where the Python standard library resides. Check each. py file to see if there is a __doc__ string, and if so, make a proper collation of its format. After your program has finished executing, you should generate a nice list. It lists which modules have the document string, and the contents of the document string. The list is appended with the name of the module without the document string.
Import os#import pdbpath2search = '/system/library/frameworks/python.framework/versions/2.7/lib/python2.7 ' Modules2check = []def findpythonfiles (Path2search): For Eachitem in Os.listdir (path2search):p Athofeachitem = Os.path.join (Path2search, Eachitem) if Os.path.isdir (pathofeachitem):p = Os.path.join (Path2search, Eachitem) #pdb. Set _trace () findpythonfiles (Pathofeachitem) else:if os.path.splitext (Eachitem) [1] = = '. Py ': Modules2check.append ( Os.path.join (Path2search,eachitem)) return modules2checkif __name__ = = ' __main__ ': templist = []modules2check = Findpythonfiles (Path2search) for eachpymodule in modules2check:f = open (eachpymodule) Isdoc = FalseFor EachLine in f:# the 4 If or elif below can ' t be reordered.# check __doc__ like R "" "SDFDSF" "" if (Eachline.startswith (' R "" "') or EACHLINE.STARTSW ITH (' "" ') ") and Len (Eachline) > 5 and Eachline.rstrip (). EndsWith ('" ""): Templist.append (eachline) break# Check the 1st line of __doc__ like R "" "Sdfsdfelif (Eachline.startswith (' R" "" ') or Eachline.startswith ('"" ")) and Isdoc = = False:isdoc = Truetemplist.append (eachline) # Check the last line of __doc__ like Sdfsdf" "" Elif Eachlin E.rstrip (). EndsWith (' "" "') and Isdoc = = True:tempList.append (eachline) Isdoc = falsebreak# Check content within R" "" and " "" "Elif Not (Eachline.startswith (' R" "") "or Eachline.startswith ('" "" ")" and Isdoc = = True:tempList.append (eachline) # Write name of module that doesn ' t has __doc__ into file Hasnodoc.txtelse:f2 = open ("Hasnodoc.txt", "A +") F2.write (' Name: ' + eachpymodule + ' \ n ') f2.close () templist = []# writ e name and content of module that have __doc__ into file hasdoc.txtif templist! = []:F1 = open ("Hasdoc.txt", "A +") f1.write (' Name: ' + eachpymodule + ' \ n ') f1.writelines (templist) f1.close () templist = []
9.9 Python Document String