gpt4 book ai didi

python - 'open("file_path")' and ' with open ("file_path")' in Python 3.8.10有什么区别,哪个最适合使用?

转载 作者:行者123 更新时间:2023-12-04 12:32:02 25 4
gpt4 key购买 nike

<分区>

我正在学习Python,我发现有两种类型的文件打开操作。第一个是,

myreadfile = open("bear.txt", "r")
content = myreadfile.read()

第二种方法是

with open("bear.txt") as file:
content = file.read()

我想知道这两种方法有什么区别,哪种方法最适合使用。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com