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

Python 3: How to check if file is open? Python equivalent of C++ ifstream.is_open() function?

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

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

C++ ifstream 为此提供了 is_open() 函数。是否有 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'