Python recursively lists file scripts and their anonymous functions in directories

1. Script example of recursively listing files in directory

To list the files in the directory, you can do the following: os.listdir()

In [1]: import os

In [4]: os.listdir('/root')


















Determine whether it is a directory:

In [5]: os.path.isdir('/home')

Out[5]: True

Determine whether it is a document:

In [7]: os.path.isfile('/etc/rc.local')

Out[7]: True

Absolute path for splicing file name:

In [8]: os.path.join('/etc/','passwd','abc')

Out[8]: '/etc/passwd/abc'

List all file scripts in the directory if:

#!/usr/bin/env python

# 2018/11/29 15:11



import os

import sys

def print_files(path):

    lsdir = os.listdir(path)

    dirs = [i for i in lsdir if os.path.isdir(os.path.join(path,i))]

    files = [i for i in lsdir if os.path.isfile(os.path.join(path,i))]

    if dirs:

        for d in dirs:


    if files:

        for f in files:

            print os.path.join(path,f)


2. Anonymous function lambda

lambda Function is a kind of minimum function that can be used to define radio function quickly.

3*5 Implementation method:

In [1]: def fun(x,y):

...:     return x * y


In [2]: fun(3,5)

Out[2]: 15

Anonymous functions are defined as follows:

In [3]: lambda x,y:x * y

Out[3]: <function __main__.<lambda>>    #Objects returned

In [4]: r = lambda x,y:x * y

In [6]: r(3,5)

Out[6]: 15

Advantages of anonymous functions:

1. When using python to write some scripts, using lambda can save the process of defining functions and make the code more concise.

2. For some abstract functions that will not be reused in other places, sometimes it is also a problem to name a function. Using lambda does not need to consider the problem of naming by hierarchy theory.

3. Use lambda to make the code easier to understand at some time.

lambda Foundation:

In a lambda statement, a colon is preceded by a parameter, and there can be multiple parameters separated by commas. To the right of the colon is the return value.

lambda statement is actually a function object.


Help on built-in function reduce in module __builtin__:


reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,

from left to right, so as to reduce the sequence to a single value.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates

((((1+2)+3)+4)+5).  If initial is present, it is placed before the items

of the sequence in the calculation, and serves as a default when the

sequence is empty.


Binary calculation of reduce:

In [19]: def add(x,y):

return x + y


In [20]: add(1,3)

Out[20]: 4

Find the sum of 1 to 100:

In [23]: reduce(add,range(1,101))

Out[23]: 5050

In [25]: reduce(lambda x,y:x + y ,range(1,101))

Out[25]: 5050

Finding factorial:

In [26]: reduce(lambda x,y:x * y ,range(1,6))

Out[26]: 120

Tags: Programming Lambda Python ssh shell

Posted on Tue, 03 Dec 2019 08:46:46 -0800 by eon201