New submission from Lumír Balhar <[email protected]>:
Hello.
The documentation about tempfile.NamedTemporaryFile[0] contains:
That name can be retrieved from the name attribute of the returned file-like
object. Whether the name can be used to open the file a second time, while the
named temporary file is still open, varies across platforms (it can be so used
on Unix; it cannot on Windows NT or later).
But after some testing on Windows, it seems that there is no problem in opening
a temporary file for the second time. A simple reproducer:
from tempfile import NamedTemporaryFile
# Open the file for the first time
tmp_file = NamedTemporaryFile(mode="w+", delete=False)
tmp_file.write("foo")
tmp_file.seek(0)
content_original = tmp_file.read()
print("Original content:", content_original)
# Open the file for the second time
with open(tmp_file.name, mode="w+") as file:
file.write("bar")
file.seek(0)
content = file.read()
print("Updated content:", content)
The output is:
Original content: foo
Updated content: bar
So the question is: do I misunderstand the documentation? Or is there any
automatic magic handling this on Windows?
I'm not a windows user and I've found this accidentally when preparing a
Windows CI job for a project.
[0]
https://docs.python.org/3/library/tempfile.html?highlight=namedtemporaryfile#tempfile.NamedTemporaryFile
----------
assignee: docs@python
components: Documentation, Windows
messages: 393082
nosy: docs@python, frenzy, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: NamedTemporaryFile opened twice on Windows
versions: Python 3.8
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44055>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com