![]() ![]() Lets look at some of the interesting functions.Ĭhain – chains multiple iterators together. The itertools module in the standard library provides lot of intersting tools to work with iterators. Problem 7: Write a program split.py, that takes an integer n and aįilename as command line arguments and splits the file into multiple smallįiles with each having n lines. Ignoring empty and comment lines, in all python files in the specified Problem 6: Write a function to compute the total number of lines of code, Problem 5: Write a function to compute the total number of lines of code inĪll python files in the specified directory recursively. Problem 4: Write a function to compute the number of python files (.pyĮxtension) in a specified directory recursively. It works similarly to zip(), but returns an iterator instead of a list. Problem 3: Write a function findfiles that recursively descends theĭirectory tree for the specified directory and generates paths of all the izip() izip() returns an iterator that combines the elements of the passed iterators into tuples. Prints all the lines which are longer than 40 characters. Problem 2: Write a program that takes one or more filenames as arguments and Move all these functions into a separate module and reuse it in other programs. The code is much simpler now with each function doing one small thing. Def readfiles ( filenames ): for f in filenames : for line in open ( f ): yield line def grep ( pattern, lines ): return ( line for line in lines if pattern in line ) def printlines ( lines ): for line in lines : print ( line, end = "" ) def main ( pattern, filenames ): lines = readfiles ( filenames ) lines = grep ( pattern, lines ) printlines ( lines )
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |