Files and functions basis (a)

Advanced application file

1. readable, writable

  1. r +: readable, writable

    Can be read but also write (try not to use), r + is added in the back

    with open('test.py', 'r+', encoding='utf8') as fr:
        date = fr.read()
        fr.write('x = 10')
        print(date)
  2. w +: write, read

    Empty file function is provided w, w + not to use

    with open('test.py', 'w+', encoding='utf8') as fw:
        fw.write('x = 10')
        date = fw.read()
        print(date)
  3. a +: appendable readable

    A has a function added, at the end of a pointer, a + function is useless

    with open('test.py', 'a+', encoding='utf8') ad fa:
        fa.write('x = 10')
        date = fa.read()
        print(date)

2. pointers applications

There are four documents built-in methods, only read (n) relates to the number of characters, seek () / tell () truncate () is the number of bytes involved

  1. seek()

    seek () method has two parameters, one is offset, one whence. represents the offset in the file offset pointer, in bytes; The whence represents the location where the file pointer, the default start of the file, from the beginning represents 0, 1 starts from the current position, the file 2 starts from the end of

  2. tell()

    tell () method is a statistical position from the beginning of the file to the current pointer location

  3. read(n)

    read (n) method reads the contents of the file, represents the number of characters n-

  4. truncate()

    truncate () method of the file is truncated, so Open file must be written, but it can not be opened with or w + w method, this will directly empty file. truncate () without parameters, the equivalent of empty files.

Files modified in two ways

First, the file is not modified to say, only covered claims.

method one

The contents of documents stored in the hard disk all loaded into memory, the memory can be modified, the modification is completed, and then covered by the memory to the hard disk

Second way

Change the contents of a file stored in the hard disk into memory line by line, write the new file after the modification is completed, the last overwrite the original file with a new file

All in all, the idea is to modify the contents of the file: Open to read the original file to open a new way to write the file, the contents of the original file to be modified, and then written to the new file, and then use the os module methods, the original file delete, rename the new file to the original file name, in order to achieve the purpose of real ones

import os
with open('原文件', 'r', encoding='utf8') as fr, \
        open('新文件', 'w', encoding='utf8') as fw:
    for i in fr:
        i = i.replace('旧的内容', '新的内容')
        fw.write(i)

os.remove('原文件') # 删除原文件
os.rename('新文件名', '原文件名') # 重命名新文件为原文件名

function

Function is a tool having a certain function modules and code reuse can improve the function of the application, is to make the code of the block. More function is an idea, not a technology.

Defined functions

Python is to use the function def keyword, the general format

def 函数名():  # 定义阶段
    '''函数注释写在这里''' # 函数相当于工具,那注释就相当于工具的说明书
    <代码块>

During the definition phase function block does not execute the function thereof, detect whether there is a syntax error only

Call functions

Call a function is to use the function function.

Function name (...) can call the function, and after the function call will execute the function body code and hit return or until completion of all the code to perform the function body ends.

Three forms of defined functions

There are three forms defined functions, namely the function empty, no reference function, the reference function has

  1. Empty function

    When we need to implement a function, but do not know how to use code to achieve, this time can be the first to write an empty function, and then go to the realization of other functions

    def func():
        pass
  2. No-argument function

    In the function definition stage, there is no argument in parentheses, for the no-argument function; defined when no parameters, so when the call is also without parameters.

    If the function code logic bodies need to rely on an external incoming value, you must have no defined function parameter.

    def func():
        print('Hello World')
    
    func()   #Hello World
  3. There are function parameters

    In the function definition stage, there are parentheses parameters, there are parameters called function; defined when there is a reference, so when the call must be passed parameters.

    If the function code logic relies on the external body of the incoming value, there have to be defined as a function parameter.

    def add(x, y):
        '''求和'''
        res = x + y
        print(res)
    
    add(1, 2)    #3

Function parameters

Before we talk about the parameters, then what is the parameter, the parameter is a function of the media outside the body to accept traditional values, in fact, the parameter is a variable name.

Then we say something about this parameter.

  1. First, talk about the parameters, and the parameters are divided into parameter arguments.

    1.1 parameter: function definition phase, the parameters defined in brackets, called formal parameters, parameter referred to, is essentially the variable name.

    1.2 arguments: the call phase function, passing the parameters within parentheses, called the actual parameter, referred to as the argument, in essence, is the variable name.

  2. Then talk is a position parameter, a position parameter and the position into a position parameter arguments.

    2.1 parameter position: the definition phase function according to the parameter defined sequentially from left to right, called the position parameter.

    2.2 the position arguments: function definition phase in accordance with the order from left to right argument definition, called argument position.

    This position parameter and the position arguments, from left to right to accept the value and the traditional values ​​one to one, no more and no less.

  3. The third is to talk about the default parameter.

    The default parameter in the definition phase, has been assigned a.

    So we call can not pass parameters, you can use the default value if the value of the parameter passed then use pass.

    However, the default parameter parameter must be placed in the rear position.

  4. The last but not least is the key argument.

    Keyword argument: when the function is called, in accordance with the parameter name to a specific value, known as keyword arguments.

    Keyword argument predetermined break position may be one-pass parameter values, but must be placed behind the key argument of argument position.

In summary, we have defined functions when the argument is best not more than three, usually when the position parameter and position argument, the default parameter and keyword arguments to use when needed use.

Function's return value

Return Value: Internal code after some function result obtained by the processing logic.

When we need to do further processing of the results of the processing function, we need to function must have a return value.

Function returns the return value to return.

return is a function of the end of the sign, to return when the function is executed, the function terminates.

return return value can return any data type.

The return value is no limit to the number returned can return multiple values ​​separated by commas, brackets without returning multiple values, return to the default tuples of the form, no return value returned None.

Guess you like

Origin www.cnblogs.com/yunluo/p/11322160.html