Python study notes --Day08

Python study notes --Day08

Today review document processing.

File review

mode description
t Text mode (default)
x Write mode, create a new file, if the file already exists it will error
b Binary mode
+ Open a
The Universal wrap mode
r Open the file in read-only mode. Pointer file will be placed at the beginning of the file. This is the default mode
rb Open a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. Generally used for non-text files such as pictures, etc.
r+ Open a file for reading and writing. The file pointer will be placed at the beginning of the file
rb+ Open a file for reading and writing. The file pointer will be placed at the beginning of the file. Generally used for non-text files such as pictures, etc.
w Open a file for writing only. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file
wb Open a file for writing in binary format only. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file. Generally used for non-text files such as pictures and so on.
w+ Open a file for reading and writing. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file.
wb+ Opens a file for reading and writing binary format. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file. Generally used for non-text files such as pictures and so on.
a Open a file for append. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written after the existing content. If the file does not exist, create a new file for writing.
from Open a file in binary format for additional. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written after the existing content. If the file does not exist, create a new file for writing.
a+ Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. It would append mode when the file is opened. If the file does not exist, create a new file for reading and writing.
ab + Open a file in binary format for additional. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file for reading and writing.

to sum up
The figure is a summary of the above centralized mode. In fact, the figures and tables are from my rookie tutorial get.
Since we already have two, and then again a table it, the more the better.

mode r r+ w w+ a a+
read + + + +
write + + + + +
create + + + +
cover + +
Pointer at the start + + + +
At the end of the pointer + +

Forehead, had to say, write, addicted, then come back for some of it.

File object properties

Attributes description
file.close Returns true if the file has been closed, otherwise false.
file.mode Return to the open file access patterns.
file.name Returns the name of the file.
file.softspace If a print output, must be followed by a space character, it returns false, true otherwise.

Disclaimer: The above contents are from the rookie tutorial, thanks rookie tutorial!
Then write some code to illustrate file operations.

#!usr/bin/python
# -*- coding: utf-8 -*-
# author: 李爽
# description:文件处理
import os


def main():
    # f = open("a.txt", "r")
    # 读取
    # str = f.read()
    # print("read():" + str)
    # 读取一行
    # str = f.readline(1024)
    # print("readline(): " + str)
    # 遍历每行
    # for line in f.readlines():
    #     print(line)
    # ---------------------------

    # 读取二进制
    # f = open("a.txt", "rb")
    # str = f.read()
    # print(str)

    # 写
    # f = open("a.txt", "w+")
    # length = f.write("haha")
    # print(length)
    # 输出指针位置
    # print(f.tell())
    # 重置指针为第一位
    # f.seek(os.SEEK_SET)
    # f.seek(0)
    # print(f.read())

    # 追加
    # f = open("a.txt", "a+")
    # f.write("lalala")
    # f.seek(os.SEEK_SET)
    # print(f.read())
    # f.close()

    # 事实上我们可以不用自己手动调用f.close()
    # python使用with自动调用close方法
    # with open("a.txt", "a+") as f:
    #     f.write("\nsdlfkjsdf")
    #     f.seek(os.SEEK_SET)
    #     print(f.read())

    # next 方法
    # with open("a.txt", "r+") as f:
    #     print(next(f))
    #     print(next(f))

    # 写入行
    # with open("a.txt", "w") as f:
    #    f.writelines("sdf\nsdlfjsfd\nlsdkjxcv")

    # 文件属性
    with open("a.txt", 'r') as f:
        print("文件名: " + f.name)
        print("文件采用的分隔符: " + str(f.newlines))
        print("文件编码:" + f.encoding) # 事实上是GBK
        print("文件报错级别:" + f.errors)
        print("文件打开模式:" + f.mode)
        print("文件缓冲:" + str(f.line_buffering))
        print("文件是否关闭:" + str(f.closed))


if __name__ == "__main__":
    main()

Small Case

Write a poem

#!usr/bin/python
# -*- coding: utf-8 -*-
# author: 李爽
# description:写古诗


def main():
    with open("古诗.txt", "w+", encoding="utf-8") as f:
        f.writelines("  静夜思\n      李白\n")
        f.writelines("床前明月光,\n疑是地上霜。\n举头望明月,\n低头思故乡。")
        f.seek(0)
        print(f.read())


if __name__ == "__main__":
    main()

Image Copy

#!usr/bin/python
# -*- coding: utf-8 -*-
# author: 李爽
# description:写古诗


def main():
    with open("我是图片.png", "rb") as f:
        with open("我是复制的.png", "wb") as copy_f:
            copy_f.write(f.read())


if __name__ == "__main__":
    main()

Write a py file and execute

#!usr/bin/python
# -*- coding: utf-8 -*-
# author: 李爽
# description:写py文件并执行
import os


def main():
    with open("我是py文件.py", "w+", encoding="utf-8") as f:
        f.write("print('hello world')")
    os.system("python 我是py文件.py")


if __name__ == "__main__":
    main()

Epilogue

Probably it is like this.
If you find my articles where there is an error or have any good ideas can contact me, we study together progress together, my email address is [email protected]

let’s do more of those!

Published 26 original articles · won praise 2 · Views 2338

Guess you like

Origin blog.csdn.net/qq_42909545/article/details/103244833