<p>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 using 'shutil.copy'. However, I get a 'permission denied' IOError:</p>

<pre><code>&gt;&gt;&gt; import tempfile, shutil
&gt;&gt;&gt; f = tempfile.TemporaryFile(mode ='w+t')
&gt;&gt;&gt; f.write('foo')
&gt;&gt;&gt; shutil.copy(f.name, 'bar.txt')

Traceback (most recent call last):
  File "&lt;pyshell#5&gt;", line 1, in &lt;module&gt;
    shutil.copy(f.name, 'bar.txt')
  File "C:\Python25\lib\shutil.py", line 80, in copy
    copyfile(src, dst)
  File "C:\Python25\lib\shutil.py", line 46, in copyfile
    fsrc = open(src, 'rb')
IOError: [Errno 13] Permission denied: 'c:\\docume~1\\me\\locals~1\\temp\\tmpvqq3go'
&gt;&gt;&gt; 
</code></pre>

<p>Is this not intended when using the 'tempfile' library? Is there a better way to do this? (Maybe I am overlooking something very trivial)</p>

You could always use
shutil.copyfileobj
, in your example:
```
```
new_file = open('bar.txt', 'rw')
shutil.copyfileobj(f, new_file)
```
```