[Python] file operation ③ (file operation | use for loop to read files | use close function to close files | with open syntax to automatically handle file closing)





1. Read the file




1. Use the for loop to read the file


Use the for loop to read the file, and each loop assigns a line of data in the file to a temporary variable. The syntax is as follows:

for 临时变量 in 文件对象:
	# 每次循环都将一行数据赋值给临时变量
	# 每次对读取的一行数据进行操作

Code example:

"""
文件操作 代码示例
"""

file = open("file.txt", "r", encoding="UTF-8")
print(type(file))  # <class '_io.TextIOWrapper'>

print("使用for循环读取文件: ")

for line in file:
    print(line)

Results of the :

D:\001_Develop\022_Python\Python39\python.exe D:/002_Project/011_Python/HelloPython/Hello.py
<class '_io.TextIOWrapper'>
使用for循环读取文件: 
Hello World

Tom

Jerry

Process finished with exit code 0

insert image description here





2. Close the file




1. close function


Call the file object #close function to close the file object;

Close the file object, you can close the program's occupation of the file;

If the file is not closed and the program keeps running, the file will be occupied by the program all the time, and other applications or this application cannot access the file normally;

Close file syntax:

文件对象.close()

Two ways to release file occupation:

  • Call the file object #close() function to release the file occupation;
  • Killing the application process occupied by the file can also release the file occupation;

2. Code example - file is occupied


Example of file usage:

"""
文件操作 代码示例
"""
import time

file = open("file.txt", "r", encoding="UTF-8")
print(type(file))  # <class '_io.TextIOWrapper'>

print("使用for循环读取文件: ")

for line in file:
    print(line)

# 休眠 1000 秒
time.sleep(1000)

In the above code, after the file reading operation is completed, it sleeps for 1000 seconds and keeps the program from exiting. At this time, the file will be occupied all the time. If you try to delete the file in the file manager, the following error will be reported;

insert image description here


3. Code example - close the file


Code example:

"""
文件操作 代码示例
"""
import time

file = open("file.txt", "r", encoding="UTF-8")
print(type(file))  # <class '_io.TextIOWrapper'>

print("使用for循环读取文件: ")

for line in file:
    print(line)

# 关闭文件
file.close()

Results of the :

D:\001_Develop\022_Python\Python39\python.exe D:/002_Project/011_Python/HelloPython/Hello.py
<class '_io.TextIOWrapper'>
使用for循环读取文件: 
Hello World

Tom

Jerry

123

Process finished with exit code 0





3. The with open syntax automatically handles file closing




1. with open syntax


Use the with open syntax to open the file, which can automatically close the file operation, avoiding the problem of file occupation caused by negligence;

Grammar description:

with open(文件路径, 打开模式) as 文件对象:
	在该语句块中借助文件对象操作文件
	操作完毕后不需要手动关闭文件
	会自动将文件对象关闭

2. Code example - with open syntax example


Code example:

"""
文件操作 代码示例
"""
import time

with open("file.txt", "r", encoding="UTF-8") as file:
    print("使用for循环读取文件: ")

    # 读取文件
    for line in file:
        print(line)

# 休眠 1000 秒避免程序退出, 用于测试文件是否被占用
time.sleep(1000)

Results of the :

D:\001_Develop\022_Python\Python39\python.exe D:/002_Project/011_Python/HelloPython/Hello.py
使用for循环读取文件: 
Hello World

Tom

Jerry

123

Try to rename the file.txt file to file1.txt, the renaming is successful, indicating that the file is not occupied;

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/131291945