temp files/directories with custom names?
<p>How to create a temporary files/directories with user defined names in python. I am aware of <a href="https://docs.python.org/3/library/tempfile.html" rel="nofollow">tempfile</a> . However I couldn't see any function with filename as argument.</p>

<p>Note: I need this for unit testing the glob(file name pattern matching) functionality on a temporary directory containing temporary files rather than using the actual file system. </p>

<p>You can use <code>open()</code> with whatever file name you need.</p>

<p>e.g.</p>

<pre><code>open(name, 'w')
</code></pre>

<p><a href="https://docs.python.org/2/library/functions.html#open" rel="nofollow">Open</a></p>

<p>Or</p>

<pre><code>import os
import tempfile

print 'Building a file name yourself:'
filename = '/tmp/guess_my_name.%s.txt' % os.getpid()
temp = open(filename, 'w+b')
try:
    print 'temp:', temp
    print 'temp.name:', temp.name
finally:
    temp.close()
    # Clean up the temporary file yourself
    os.remove(filename)
</code></pre>

<p>Or you can create a temp directory using <code>mkdtemp</code> and then use <code>open</code> to create a file in the temporary directory. </p>

python py.test temporary-files pyunit