Python 3:如何检查文件是否打开?Python等价于C++ifstream.is_open()函数



在Python3中打开文件后,如何检查文件是否已正确打开?

with open("filename" as f:
# check that file is opened correctly here

C++CCD_ 1为此提供了CCD_ 2函数。有没有相当于C++ifstream::is_open()函数的Python?

在C中,一个人会做

FILE *fd = fopen(...)
if(fd != NULL) ...
f = open('file.txt')
if f.closed:
print 'file is closed'
if not f.closed:
print 'file is opened'

最新更新