热线电话:13121318867

登录
2019-03-12 阅读量: 692
如何理解os模块的close函数?

使用open()打开的文件只能通过close()关闭。但是通过os.popen()打开的文件可以用close()或os.close()关闭。如果我们尝试用open()关闭一个打开的文件,使用os.close(),Python就会抛出TypeError。

代码:

import os

fd = "GFG.txt"

file = open(fd, 'r')

text = file.read()

print(text)

os.close(file)

输出:

Traceback (most recent call last):
File "C:\Users\GFG\Desktop\GeeksForGeeksOSFile.py", line 6, in
os.close(file)
TypeError: an integer is required (got type _io.TextIOWrapper)

注意:由于文件或权限权限不存在,可能不会引发相同的错误。

7.6719
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子