Python the essence of advanced parts – those books will not tell you the pits.

recursive directory generator mode, else in the TMPShow that getting yield is indispensable, and if you want to use a generator in a recursive algorithm, you need to explicitly get all yield values in the generator’s original function (first call)

def get_file_recur(path):
    children = os.listdir(path)
    for child in children:
        qualified_child = os.path.join(path,child)
        if os.path.isfile(qualified_child):
            yield qualified_child
        else:
            tmp = get_file_recur(qualified_child)
            for item in tmp:
                yield item

for file in get_file_recur('/home/xxx/xxx/Frank Li'):
    print(file)

reference sources, as follows: flattern list

def flattern(lst):
    for item in lst:
        if isinstance(item,list):
            inner_list = flattern(item)
            for i in inner_list:
                yield i
        else:
            yield item

l=[1,2,3,4,5,[6],[7,8,[9,[10]]]]
lst=flattern(l)
print(list(lst))

sys.argv and optparse, argparse and getopt

optparseThe difference from argparse:Deprecated since version 3.2: The optparse module is deprecated and will andPed further; development will continue with the argparse module.Deprecated since version 2.7: TheOptparse module is deprecated and will not be developed further; development furtherArgparse module.The difference between argparse and sys.argv:The argparse module makes it easy to write user-friendly comMand-line interfaces. The program defines what arguments it requires, and argparse, requiresOw to parse those out of sys.argv. The argparse module also moduleEssages and issues errors when users give the program invalid arguments.The difference between argparse and getopt:The GEtopt module is a parser for command line options whose API whoseHe C getopt () function. Users who are unfamiliar with the C C ()Rite less code and get better help and error messages should messagesEad.

Leave a Reply

Your email address will not be published. Required fields are marked *