Home >Backend Development >Python Tutorial >python file read and write operation source code analysis

python file read and write operation source code analysis

WBOY
WBOYforward
2023-04-29 13:52:061141browse

    Case of file write operation

    # 打开文件(只写模式)
    file = open('example.txt', 'w')
    # 写入单行数据
    file.write('Hello World!\n')
    # 写入多行数据
    lines = ['这是第一行\n', '这是第二行\n', '这是第三行\n']
    file.writelines(lines)
    # 关闭文件
    file.close()

    Code explanation

    First of all, in the first line of code we open a file named example.txt file, and adopt w mode, which means write-only mode. If the file does not exist, it will be created automatically. If the file already exists, the original content will be cleared.

    Next, in the third line of code, we use the write() method to write a line of text to the file. Note that \n is used to indicate a line break. symbol.

    In the sixth line of code, we put multiple lines of text into a list, and then use the writelines() method to write all elements in the list to the file at once.

    Finally, in the ninth line of code, we close the file using the close() method, which releases the file handle and ensures that the file is not accidentally modified after use.

    It should be noted that when using the write() method to write data, this method returns the number of characters (or bytes) successfully written, not the number of written Content. In addition, the file should be closed promptly after writing the data to avoid data loss.

    In Python, you can use the following three methods to read files

    • read()method: to specify the number of characters (or words section size) reads the file contents and returns a string.

    # 打开文件(只读模式)
    file = open('example.txt', 'r')
    # 读取整个文件
    content = file.read()
    # 关闭文件
    file.close()

    In the above code, we read the contents of the entire file using the read() method and assigned it to the variable content. It is important to note that if the file is very large, reading the entire file at once may cause out of memory problems. To avoid this, use one of two methods.

    • readline()Method: Read the file content in one line and return a string. Each time this method is called, it reads the next line from the file. When the end of the file is reached, this method returns an empty string.

    # 打开文件(只读模式)
    file = open('example.txt', 'r')
    # 读取单行数据并打印
    line = file.readline()
    print(line)
    # 关闭文件
    file.close()

    In the above code, we read the first line of the file using the readline() method and assigned it to the variable line . It takes multiple calls to this method to read all lines of the file.

    • readlines()Method: Read all the lines in the entire file at once in the form of a list, with each line as an element in the list.

    # 打开文件(只读模式)
    file = open('example.txt', 'r')
    # 读取所有行
    lines = file.readlines()
    # 遍历所有行并打印
    for line in lines:
        print(line)
    # 关闭文件
    file.close()

    In the above code, we have read all the lines of the entire file using the readlines() method and assigned them to the variable lines . Then, we use for to loop through all the lines and print out the contents of each line.

    No matter which method is used, the file needs to be closed in time after reading the file to release system resources and ensure that the file is not accidentally modified.

    The above is the detailed content of python file read and write operation source code analysis. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete