picture

Blogger Profile: Former employee of Tencent, an Internet security giant, employee of Venustech, an Internet security giant, expert blogger of Alibaba Cloud development community, high-quality creator of Java basic notes on WeChat public account, high-quality creative blogger of csdn, entrepreneur, knowledge sharer, welcome to pay attention , like, favorite.


1. Background

  Python is an easy-to-learn, powerful programming language. It provides efficient high-level data structures as well as simple and efficient object-oriented programming. Python's elegant syntax and dynamic typing, as well as the nature of an interpreted language, make it an ideal language for scripting and rapid application development on most platforms. Now let's introduce the knowledge related to reading files in python.


2. Read the file

1. read() method

  The read() method is used to read the specified number of bytes from the file. If no parameter is given or the parameter is negative, the entire file content is read. The syntax format is as follows:
(1) size is read from the file number of bytes
(2) This method returns the string read from the file

文件对象名.read([size])

  Example: Use the read() method to read the "testfile.txt" file.

with open('testfile.txt','r'as file:    #以只读方式打开原有的名为“testfile.txt”的文件
    line = file.read(10)        #读取前10个字节
    print(line)            #输出前10个字节
    print('*'*30)            #输出30个*用于分隔
    content = file.read()        #读取文件中剩余的所有内容
    print(content)            #输出

  The results are as follows.

picture

2. readline() method

  The readline() method is used to read the entire line from the file, including the "\n" character. If a non-negative parameter is specified, it means to read a string of the specified size. Its syntax is as follows:

文件对象名.readline([size])

  Example: Use the readline() method to read the "testfile.txt" file.

with open('testfile.txt','r'as file:    #以只读方式打开原有的名为“testfile.txt”的文件
    line = file.readline()        #读取一行
    print(line)            #输出
    print('*'*30)            #输出30个*用于分隔
    line = file.readline(10)        #读取下一行的前10个字符
    print(line)            #输出

  The results are as follows.

picture

3. readlines() method

  The readlines() method is used to read all lines (until the end character EOF) and return a list, each element in the list is a line of data in the file, and its syntax is as follows:

文件对象名.readlines()

  Example: Use the readlines() method to read the "testfile.txt" file.

with open('testfile.txt','r'as file:    #以只读方式打开原有的名为“testfile.txt”的文件
    content = file.readlines()    #读取所有行并返回列表
print(content)            #输出列表
print('*'*60)            #输出60个*用于分隔
for temp in content:        #遍历列表
    print(temp)     #输出列表每个元素

  The results are as follows.

picture
with open('testfile.txt','r'as file:    #以只读方式打开原有的名为“testfile.txt”的文件
    for line in file:            #遍历文件的所有行
        print(line)     #输出行

  The results are as follows.

picture

  Example: Copy the contents of the file "testfile.txt" to another file "copy.txt".

with open('testfile.txt','r'as file1,open('copy.txt','w'as file2:    #打开两个文件
    file2.write(file1.read())        #将从“testfile.txt”中读取的内容写入到“copy.txt”中

The results are as follows.

picture

3. Reference

1. Liao Xuefeng's official website
2. Python official website
3. Python programming case tutorial


4. Summary

  The above is the relevant knowledge about reading files in Python. You can refer to it. If you think it is good, welcome to like, favorite, and watch it. Welcome to WeChat search and follow the basic notes of java. The relevant knowledge will be updated continuously, and everyone will make progress together.