KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm writing a Python script that needs to write some data to a temporary file, then create a subprocess running a C++ program that will read the temporary file. I'm trying to use NamedTemporaryFile for this, but according to the docs, 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). And indeed, on Windows if I flush the temporary file after writing, but don't close it until I want it to go away, the subprocess isn't able to open it for reading. I'm working around this by creating the file with delete=False , closing it before spawning the subprocess, and then manually deleting it once I'm done: fileTemp = tempfile.NamedTemporaryFile(delete = False) try: fileTemp.write(someStuff) fileTemp.close() # ...run the subprocess and wait for it to complete... finally: os.remove(fileTemp.name) This seems inelegant. Is there a better way to do this? Perhaps a way to open up the permissions on the temporary file so the subprocess can get at it?
Tags (comma-separated)
Save Edits
Cancel