Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Python for File Systems!
You have completed Python for File Systems!
Preview
Python has an entire module dedicated to temporary files, directories, and friends.
Python Documentation
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Python has an entire module that's
dedicated to temporary files,
0:00
directories and.
0:04
These tools are extremely useful when
you're working on things that should be
0:05
saved to disk to reduce
their RAM footprint.
0:08
But either need to be thrown away
when you're done processing them, or
0:10
when the user decides
to cancel the process.
0:13
While there's quite a bit in the module,
we're only going to focus on two things,
0:16
temporary files and temporary directories.
0:20
Let's start with the directory,
so I've imported os,
0:22
I also want to import tempfile.
0:24
And I'm going to use with
0:27
tempfile.TemporaryDirectory() as
tmpdirname.
0:30
I'm gonna print,
Created temporary directory named,
0:38
something, and
let's format that with tmpdirname.
0:44
And we are going to say
with open os.path.join
0:51
tmpdirname and temp_file.txt,
0:59
we're gonna write to that as f.
1:04
I'm going to do f.write hello and
then a backslash n,
1:09
and then I'm gonna call input right here,
but
1:14
let's do input here actually,
inside of the first with there.
1:18
So, I run this, and
we get out this file here,
1:24
created directory named var,
folders, blah, blah, blah, blah.
1:27
Let's go to that, /var/folders,
and what was that, cm?
1:32
And then, there, and then, T, and then,
1:41
tmpa8_8, and there's our temp file.
1:47
If we open that up, we've got out
hello right there, so that's cool.
1:52
If you're in Windows,
1:57
this may not work, by the way, just due
to how windows handle temporary files.
1:59
So, your mileage may vary on that one,
so that's cool.
2:04
And what's even cooler is, I'm back over
here, and I wanna talk real quick here.
2:09
The 6 here is because I did H-E-L-L-O,
5, and then the new line character, 6.
2:12
So just so you know,
that's what that 6 is there for.
2:17
If I press Enter here inside of
this input, the directory and
2:21
the file are both gone.
2:26
I was moved up a directory because that
one exists, the one I was in no longer
2:27
does, and that file is gone,
the whole thing has been removed.
2:31
I guess that's kind of the point of the
whole temporary thing, though, isn't it?
2:34
One thing that I wanna point out is
that we didn't create a temporary file,
2:40
only a temporary directory.
2:44
Anything that we would have added to
the directory would be removed too,
2:46
no matter what.
2:48
So you could download
an archive with Python,
2:49
decompress it into your temporary
directory, do whatever work you needed,
2:51
and then it would all be cleaned up
afterward, which is pretty cool.
2:54
What if we only needed the temporary file,
though?
2:58
Believe it or not,
it's pretty much the same kind of code.
3:00
Now, what I'm about to show you may or
may not work on your operating system.
3:03
It won't work here on my Mac one,
but I know I've used it before and
3:07
it worked fine, so just give this a try.
3:12
With tempfile.TemporalyFile as tmpfile,
3:15
and we're gonna do tempfile.write, and
we're gonna write some binary to it here,
3:21
we'll write hello, And
then we will seek back to 0,
3:28
and then we'll use tempfile.read to
try to read out what's in there.
3:35
And I get an attribute error, but
I've had this work on other systems, so
3:41
give them a try as well.
3:44
If you don't want to use the with
content manager that's right there,
3:46
you can do this just like
it's a normal file object.
3:49
So you could say,
fp = tempfile.TemporaryFile(),
3:52
and you do fp.write, again,
we'll do some bytes.
3:57
And then you can do fp.close(),
and the file is gone.
4:05
Now, we can't find these temporary
files on our system, though.
4:10
They're not guaranteed to have visible
directory entries on any operating system.
4:12
If you need to get the file name, though,
4:17
you need to use a different
type of temporary file.
4:19
So this time, we'll do fp =
tempFile.namedTemporaryFile().
4:22
And now if we were to do fp.name,
we would get a path, and we could go and
4:29
we could look at that file.
4:32
If we were to open up this file,
4:34
we'd be able to see any contents in
it that'd been flushed to the disk.
4:35
Again, though,
4:38
once the file is closed, at least by
default, the file will be deleted.
4:39
These temporary file and
directory solutions are really handy for
4:43
a lot of different scenarios.
4:46
I know I've used them for
handling archives for
4:48
processing temporary file
uploads from users, and
4:50
intermediate files during long
running processes, like image editing.
4:53
I'm sure you'll find all sorts of uses for
them yourself.
4:56
Okay, I think we can build
something useful with our file and
4:59
directory handling tools.
5:02
In the next stage, let's build a small app
that'll help us build other apps faster.
5:03
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up