Creating Temporary Files and Directories
00:00 In this lesson, I’ll show you how to work with temporary files and directories, which can be really valuable tools—especially for testing—because there might be situations where you want to write functions or classes or whatever, that operate on files and directories, but you want to be able to test them without risking corrupting your filesystem.
tempfile.TemporaryDirectory(), and that creates a temporary directory and just returns it for your use. So, very simple. I won’t really be using a sample directory for this because, of course, I’ll be creating and then deleting immediately all of the temporary files and directories that I use for this lesson. As usual, you need to get your imports going, which in this case is just
tempfile, but I’m also going to import
os so that I can just show you that I’m in an empty directory.
And then, if you don’t want to mess with this directory, you’ll need to use some temporary files. So, since
tempfile supports the context manager protocol, it’s very easy to do that by saying
with tempfile.TemporaryFile(), and then I’m going to pass in the mode just
'w+', which means read and write, as opposed to
'w+b', which means read and write bytes, because I want to just read and write strings.
01:28 And I’m not going to use the other parameters of this. There are a lot of other parameters, but check those out on your own time if you’d like to. For this demonstration, they won’t be super important.
with tempfile.TemporaryFile('w+'), in read and write mode,
as temp, I can just say
temp.write—just as I would with any other file—
"I'm a temp file!" And then, I’m actually going to use the
.seek() function to go all the way back to the beginning of the file so that I can then print out all the contents of the file.
print(temp.read()), and as you can see, it correctly reads,
I'm a temp file! But if I try to say
temp.read() here, it’ll say
I/O operation on a closed file. And in fact, it’s not only a closed file, it’s also a nonexistent file because
temp is now just an IO wrapper that points to an empty file.
Similar things tend to happen when you use
tempfile.TemporaryDirectory(), except the temporary directory—again, I won’t really use all the parameters here, I’ll just use a base directory—except the temporary directory is, in fact, just a string.
My system is putting this in a strange directory. Most of the time it will be in, like,
/tmp (temp), or something like that, but for me, it’s in this kind of odd directory, just cause of quirks in how my system works.
You could start doing whatever you wanted to do, just as you normally would with a directory. But now, of course, when I say
os.path.exists() outside of this
with block, that will give me
False, because at the end of this context managed block, the resources are all deleted and they have no lasting effect on your filesystem.
So that’s tremendously convenient and important in both of these patterns because it means that you can essentially do whatever you want with these files or directories, and none of it will have any lasting effect on your system, beyond the end of this
03:45 So that’s why temp files and temporary directories are important and useful: because there are a lot of cases where you might want to do things with files or test things with files, but you don’t actually want to risk, say, deleting a really important file somewhere in your filesystem.
Become a Member to join the conversation.