with语句打开文件

对文件操作使用with语句,将减少代码量,同时不用担心文件忘记关闭的问题(with会自动关闭文件)
with open(r'文件名','r') as file:
    for each_line in file:
        print(each_line)

 

你可能感兴趣的:(python,文件操作)