python file read and write

  • DHCP Summary
    Concept : DHCP—Dynamic Host Configuration Protocol Dynamic Host Configuration Protocol Protocol—Standard TCP/IP
    for dialogue between communication parties —Transmission Control Protocol—Provides a secure protocol UDP—Does not provide a secure protocol

First, read the file

  • 1—Specify a path
    Relative path projects often use absolute paths
    Absolute paths
  • 2—specified mode
    read file -r rb (binary)
    write file -w
    append content -a
  • 3—with establishes a context for open, as long as it leaves the context, it will be released immediately
  • 4—Reading a file is divided into five steps
    1) Open the file
    2) Determine the size (omitted by python)
    3) Allocate memory
    4) Read the file
    5) Close the file
  • 5 - Exception mechanism - a means of handling unexpected situations that occur during the running process of the program,
    because not all problems can be found when writing the program to debug the
    code that may be in a try situation - except to prevent the program from crashing, continue to Post-execution
    According to the error handling method, there can be different writing methods. If the error mechanism is the same, they can be put together.
    Inconsistencies need to be separated.
import time


def main():
    try:
        with open('hallo.txt', 'r', encoding='utf-8') as fs:
            mylist = fs.readlines()
            for line in mylist:
                print(line, end='')
                time.sleep(0.25)
    # 捕获2种错误,这里是一个元组的表达形式
    # except (FileNotFoundError, IOError):  
        # print('指定的文件无法打开。')
    except FileNotFoundError as error:
        print(error)
        print('指定的文件无法打开。')
    except IOError:
        print('读写文件时出现错误。')
    print('程序执行结束。')


if __name__ == '__main__':
    main()

2. Write files

  • Similar to reading a file, just replace 'r' -> 'w'
def main():
    try:
        with open('music-name.txt', 'w', encoding='utf-8') as file:
            file.write('刘明湘的《漂洋过海来看你》真好听,耳朵都怀孕了。\n')
            file.write('蔡佩轩的《我们不一样》,真好听,耳朵都怀孕了。')
    # except (FileNotFoundError, IOError):  # 捕获2种错误,这里是一个元组的表达形式
        # print('指定的文件无法打开。')
    except FileNotFoundError as error:
        print(error)
        print('指定的文件无法打开。')
    except IOError:
        print('读写文件时出现错误。')
    print('程序执行结束。')


if __name__ == '__main__':
    main()

3. Additional files

  • Similar to writing a file, just append 'w' —> 'a'
def main():
    try:
        with open('music-name.txt', 'a', encoding='utf-8') as file:
            file.write('刘明湘的《漂洋过海来看你》真好听,耳朵都怀孕了。\n')
            file.write('蔡佩轩的《我们不一样》,真好听,耳朵都怀孕了。\n')
    # except (FileNotFoundError, IOError):  # 捕获2种错误,这里是一个元组的表达形式
        # print('指定的文件无法打开。')
    except FileNotFoundError as error:
        print(error)
        print('指定的文件无法打开。')
    except IOError:
        print('读写文件时出现错误。')
    print('程序执行结束。')


if __name__ == '__main__':
    main()

Fourth, read and write binary files

def main():
    try:
        with open('../momo/linzhiling.jpg', 'rb') as file1:
            data = file1.read()
            print(data)
            print(type(data))
        with open('../kaka/beuty.jpg', 'wb') as file2:
            file2.write(data)
    except FileNotFoundError as error:
        print(error)
        print('指定的文件无法打开。')
    except IOError:
        print('读写文件时出现错误。')
    print('程序执行结束。')


if __name__ == '__main__':
    main()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325777292&siteId=291194637