1. Examples of scripts that recursively list files in a directory
The list of files in the directory can be done by using the following method:Os.listdir ()
In [1]: import OS
In [4]: os.listdir ('/root ')
OUT[4]:
['. TCSHRC ',
'. Bash_history ',
'. BASHRC ',
' ENV ',
'. Cache ',
'. config ',
'. CSHRC ',
'. Bash_logout ',
' Python ',
'. SSH ',
' Shell ',
'. Bash_profile ',
'. Ipython ',
'. Viminfo ',
' Dictionary.txt ',
' 1.txt ']
Determine if it is a directory:
In [5]: os.path.isdir (' home ')
OUT[5]: True
To determine whether a file is:
In [7]: os.path.isfile ('/etc/rc.local ')
OUT[7]: True
The absolute path to the name of the stitching file:
In [8]: os.path.join ('/etc/', ' passwd ', ' abc ')
OUT[8]: '/ETC/PASSWD/ABC '
List all file scripts under directory if:
#!/usr/bin/env python
# @Time: 2018-01-05 15:11
# @Author: fengxiaoqing
# @file: listdir.py
Import OS
Import Sys
def print_files (path):
Lsdir = Os.listdir (path)
dirs = [i-I in Lsdir if Os.path.isdir (Os.path.join (path,i))]
Files = [i-I in Lsdir if Os.path.isfile (Os.path.join (path,i))]
If dirs:
For D in dirs:
Print_files (Os.path.join (path,d))
If files:
For f in Files:
Print Os.path.join (path,f)
Print_files (Sys.argv[1])
2. anonymous function lambda
A lambda function is a minimal function that quickly defines a single selection and can be used wherever a function is needed.
3*5 Implementation Method:
In [1]: def fun (x, y):
...: return x * y
...:
In [2]: Fun (3,5)
OUT[2]: 15
anonymous function definition if the following:
In [3]: Lambda x,y:x * y
OUT[3]: <function __main__.<lambda>> #返回的对象
In [4]: r = Lambda x,y:x * y
In [6]: R (3,5)
OUT[6]: 15
Advantages of anonymous functions:
1. Using Python to write some scripts, using lambda eliminates the process of defining a function, making the code more streamlined.
2. For some abstract functions that are not reused by other places, sometimes a function name is a problem, and using lambda does not require hierarchical theory to consider naming problems.
3. Using lambda makes the code easier to understand at some point.
Lambda Basics:
In a lambda statement, the colon is preceded by a parameter, can have multiple, separated by commas, and a return value to the right of the colon.
A lambda statement is actually built as a function object.
Help (reduce)
Help on built-in function, reduce in module __builtin__:
Reduce (...)
Reduce (function, sequence[, initial]), value
Apply a function of arguments cumulatively to the items of a sequence,
From left-to-right, so-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's placed before the items
Of the sequence in the calculation, and serves as a default when the
Sequence is empty.
(END)
Reduce two Yuan calculation:
in [+]: def add (x, y):
return x + y
....:
in [+]: Add (1,3)
OUT[20]: 4
Sum of 1 to 100 added:
In [All]: Reduce (Add,range (1,101))
OUT[23]: 5050
in [+]: reduce (lambda x,y:x + y, Range (1,101))
OUT[25]: 5050
To find factorial:
in [+]: Reduce (lambda x,y:x * y, Range (1,6))
OUT[26]: 120
Exercises
1. Receive several numbers from the terminal, require the use of the filter () function, the input is not a number of values to be eliminated (the user input is random, when we want to receive a number, he may enter a string to ask when the user input is not a number, it is removed)
2. After receiving several space-separated strings from the terminal, and then removing all characters other than 26 characters, print to the screen
requirements: Using the map () function, the map () function receives two parameters, one is the function, and the other is Iterable,map the passed function to each element of the sequence and returns the result as a new iterator.
3. Receive several strings separated by a space from the terminal
(1). A space-delimited string for a value that the user wants to enter
(2). The user accidentally presses the letter key when typing
examples such as the following
"afda111 dafd222 3dfad33 4ss4dd4"
This string, in fact, is what the user wants to ask 111 222 333 444 and
Requirements: Remove the letters from each value and calculate the
Tip: Using the reduce function, combined with the methods used in the first two questions, can be easily calculated
Python recursively lists file scripts and their anonymous functions in the directory