我有一个文件对象file
,是通过以下方式打开的:
with open("text.txt","rb") as file:
# read some line
file.write("texte") # I know that doesn't work
我想知道是否可以重新打开这个文件,因为Python已经获取了所有关于它的信息,但是它显示这个文件已经关闭了。
我正在尝试读取文件中的一些行,在文件末尾写入新的一行,因此我想以追加模式重新打开这个文件。
>>> with open("test.txt","wb") as fich:
... fich.write("test In ")
...
>>> fich
<closed file 'test.txt', mode ‘wb' at 0x7f3f49a3a4b0>
>>> fich.open
Traceback (most recent call last):
File “<stdin>", line 1, in <module>
我正在调用一个需要文件句柄作为参数的函数,因此无法像第一次那样重复使用open(),因为函数内部未知文件名。
我能重新打开句柄本身吗?因为我可以使用同样的方法并重新打开它:
with open("filename") as anotherfile:
with open("test.txt", "a") as fich:...
- Hans Musgrave