One, file operation
1. Open the file
f = open('file path') The default opening method is r, and the default opening encoding is the default encoding of the operating system
r w a (r+ w+ a+) , if you turn on mode+b, you don't need to specify the encoding
encoding utf-8, gbk
2. Manipulate files
read
read does not pass parameters, which means read all
Pass parameter, if it is opened in r mode, the parameter refers to how many characters to read
Pass parameter, if it is opened in rb mode, the parameter refers to how many bytes to read
reading
Read line by line, one line at a time, will not stop automatically
way of for loop
Read line by line, starting from the first line, read one line at a time, and stop after reading no more
Write
write write content
close file
f.close()
with open() as f:
Modify the file:
import them
remove them
os.rename
function
definition
Keyword def function name (parameter):
parameter:
positional parameters
*args dynamic parameters: receive the redundant parameters of all positions passed at the time of the call
keyword arguments
**kwargs dynamic parameters: receive all the redundant parameters passed by keyword when calling
return value
return stops the execution of a program and returns the parameter
No return value, returns None by default
has a return value
return multiple values
transfer
Called keyword function name (argument)
Pass parameters:
By location
Pass by keyword
receive return value
no return value not received
Has a return value, received in a variable
have multiple return values
Received as a variable, all return values will be organized into a tuple
Use multiple variables to receive, how many return values must be used to receive as many variables
The concept of functional first-class objects
function name --> the memory address of the function
The function name can be used as the parameter of the element function of the container class, and the return value can also be assigned --> variable
Closures and Decorators
The meaning of closure: the inner function refers to the variable of the outer function
Application of Closures: Decorators
decorator
Add functions in front of the original function without changing the calling method of the function
1 def timmer(f): 2 def inner(*args,**kwargs): 3 4 ret = f(*args,**kwargs) 5 6 return ret 7 return inner