我用Python2.7代码创建了一个内置的文件对象,它返回给我一个文件对象<type 'file'>
file(os.path.join('/tmp/test/', 'config.ini'))
与我在Python3.7中更改的代码相同,如下所示,它返回<class '_io.TextIOWrapper'>
open(os.path.join('/tmp/test/', 'config.ini'))
如何在python 3中获取文件对象类型
发布于 2019-07-04 08:58:46
您可以以完全相同的方式使用它们。
file = open(os.path.join("/tmp/test/", "config.ini"))
print(file.read()) # Will print file contents
或者使用pathlib:
from pathlib import Path
file = Path("/tmp/test") / "config.ini"
print(file.read_text()) # will do the same
您正在寻找的不是文件对象。您正在寻找一个允许您读写文件的对象。
https://stackoverflow.com/questions/56884172
复制