table of Contents
Advanced application file
r + to readable and writable, and is added in the back
w + function empty file is provided w
+ A A there is an additional function, a pointer is at the end of, a + a reading function is useless
Files modified in two ways
The first
with open('test', 'r', encoding='utf8') as fr, \
open('test_swap', 'w', encoding='utf8') as fw:
data = fr.read()
data = data.replace('sb', 'dsb')
fw.write(data)
import time
time.sleep(5)
os.remove('test.py') # 删除文件
os.rename('test_swap.py', 'test.py') # 重命名文件
The second
with open('test', 'r', encoding='utf8') as fr, \
open('test_swap', 'w', encoding='utf8') as fw:
for i in fr:
i = i.replace('sb', 'dsb')
fw.write(i)
os.remove('test.py') # 删除文件
os.rename('test_swap.py', 'test.py') # 重命名文件
Defined functions
Before the code block, and function names written on def
Function parameters
Parameter: the parameter definition phase only, in the form of parameters, use had nothing, accounts for only a position, with descriptive sense
Argument: only when invoked only argument is that the actual parameters, has a specific value
Location parameter: write a one parameter, called the location parameter
Location argument: a write past a position called the argument
Position and a position parameter argument is one to one from left to right
Default parameter: no transmission parameters, a default value may be used directly, used to pass parameters you pass values, the default parameter must be placed behind the position parameter
Keyword arguments: According to the parameter name can be given a specific value, you can break the rules must be one to one location parameter
Function's return value
return : you can return any data type, but it defaults is a tuple