I am attempting to use the \'tempfile\' module for manipulating and creating text files. Once the file is ready I want to save it to disk. I thought it would be as simple as usi
The file you create with TemporaryFile
or NamedTemporaryFile
is automatically removed when it's closed, which is why you get an error. If you don't want this, you can use mkstemp
instead (see the docs for tempfile).
>>> import tempfile, shutil, os
>>> fd, path = tempfile.mkstemp()
>>> os.write(fd, 'foo')
>>> os.close(fd)
>>> shutil.copy(path, 'bar.txt')
>>> os.remove(path)