Copying, Moving, and Renaming Files
There are several useful functions for copying and moving files. For copying, there are two options—or, actually, three options with the
shutil (shell utilities) module, which you’ll have to import.
So, for example, things like its last modification time, and so on, will not be preserved. That will all just be reset to the baseline values. If you do need the metadata, you can use the
shutil.copy2() function, which has identical syntax, it just copies the metadata, as well.
So, those are distinct use cases. Then you have
shutil.copytree(), which copies full directory trees. This is what you should use if you want to copy directories in any capacity, but just keep in mind that it will copy the whole tree rooted at
src_dir, and it will copy it into the destination directory.
os.rename() really does essentially the same thing because moving and renaming are, really, two sides of the same coin, so to speak. It renames the file or directory with the old and the new name.
01:48 So, those are all pretty good and convenient. Let’s take a look at the sample directory, real quick, before moving into the Python REPL. The sample directory has a couple of folders, I’ve used this one before: two folders, a few Python files in each, and then a couple of text files at the top-level.
03:01 So, as you can see there, the modification times are different, not by a huge factor, but that’s because I just created these files recently. And then the atime, as well, is pretty different, And the creation time is different, too, different ID numbers.
So a lot of these are quite different, even though they’ve been copied. If you really want to copy this metadata and preserve the modification times and stuff like that, then you’ll have to use
04:04 And as you can see, this atime and the modification time are now exactly the same. What has remained different though, are the ctime, the absolute creation time, which makes sense because you don’t want to copy over the creation time, because that just doesn’t make sense, right?
04:21 This file was still created by the copy, even though it’s supposed to have the same metadata as this. Then the ID number, which is a unique system identifier really should be different as well, because otherwise, your OS won’t know what to refer to these files by, because it just uses bare numbers to get these files.
05:23 version, just to make sure that you see that they are, in fact, identical. So, same file, same everything, all copied over, just fine. Just be careful with this because it copies the whole directory tree there.
05:34 Any subdirectories, those will also be copied. Sometimes that might not be exactly what you want. Maybe you want a little more granularity, and if that’s the case, you’ll have to do a little more work and iterate through these directories on your own.
So now, let’s talk about moving and renaming files because those are really just the same operation. So your options are
shutil.move() and let’s try to move
but it now shows up in
folder_1/. So that works just like you might expect it to. The thing that might be difficult, that I sometimes find troublesome with this is you might be tempted to say something like,
What I would do is write out this full path as the destination of the
move(), because otherwise, this could become confusing to maintain, just because it might not be totally clear where you’re trying to actually move this thing.
So, a lot of stuff there that all works just fine. And both of these functions work on directories as well. If I want to just rename
'folder_1' to be
'old_folder_1', just so that it’s clear what happened there—when I copied it earlier—then I can do that with no issues.
Become a Member to join the conversation.