Python file reading and writing case - copying large and small files

Welcome to follow the blogger python old bird or go to " Python Self-study Network ", starting from the basic introductory free course, and gradually learning the python full-stack system course, suitable for beginners to master full-stack development.


Free Column Portal: " Python Basic Tutorial "

Table of contents

1. Copy small files

1.1 "The specific steps of copying small files

1.2" Code example

2. Copy large files

2.1 "Specific steps for copying large files

2.2 "Code Example


Requirements: Use code to implement the file copy process. (copy the source file into a new file)

1. Copy small files

Open an existing file, read the entire content, and write to another file

1.1 "The specific steps of copying small files

  1. Use the open function to open two files, one is the source file and the other is the target file, the original file is opened in read-only mode, and the target file is opened in write-only mode
  2. Use the read method to read the content of the source file at one time, and then write the read content directly to the target file
  3. close source and object files

1.2" Code example

Prepare a source file with the following content:

code:

# 1.打开文件
file_read = open("HELLO", encoding="UTF_8") # 只读方式
file_write = open("HELLO(复件)", "w", encoding="UTF_8")  # 只写方式

# 2. 读、写
text = file_read.read()
file_write.write(text)

# 3. 关闭文件
file_read.close()
file_write.close()

Note : It can be opened without writing encoding="UTF_8", indicating that the Python encoding is correct, so I don't need to add this encoding code like this.

Results of the:

 


2. Copy large files

  • Large files are not suitable for one-time reading, because the source file is too large and one-time reading will cause too much pressure on the memory
  • Open an existing file, read the complete content line by line, and write to another file sequentially

2.1 "Specific steps for copying large files

It is very similar to the above steps of copying small files, only need to modify the second step

  • 1. Use the open function to open two files, one is the source file and the other is the target file. The original file is opened in read-only mode, and the target file is opened in write-only mode.
  • 2. Use an infinite loop and judge the code to be read line by line, use readline() to read the content of the source file line by line, and then directly write the order of the read content into the target file
  • 3. Close the source and target files

2.2 "Code Example

Preparation: Prepare a set of source file data

code:

# 1.打开文件
file_read = open("HELLO", encoding="UTF_8")  # 只读方式
file_write = open("HELLO(复件)", "w", encoding="UTF_8")  # 只写方式

# 2. 读、写
while True:
    text = file_read.readline()



    # 判断是否读取到内容
    if not text:
        break

    file_write.write(text)


# 3. 关闭文件
file_read.close()
file_write.close()

Execution result: view the copy content

If you don’t understand the five file operation methods/functions mentioned above, open open(), close close(), read read(), write writ(), and read readline() line by line, you can go to the previous one. Chapter Python file operations .

Guess you like

Origin blog.csdn.net/weixin_48728769/article/details/126952441