Manipulating Files and Directories
![]() |
Hello and welcome to the fifth episode of the Software Carpentry lectures on handling files and directories in Python. We’ll now look at creating and deleting directories, and moving directories and files around. |
![]() |
Let us assume we are in an empty directory called user. And we want to create one called data. For this we use the mkdir function from the os module. |
![]() |
|
![]() |
We can use listdir to see that we indeed have a new directory called data. |
![]() |
And, that this new directory is empty. |
![]() |
Now, if we try to use mkdir again… |
![]() |
…we get an error since it already exists. |
![]() |
Suppose we want to make a nested set of directories for a more complex organisation of files. Let’s give that a try. |
![]() |
It fails. |
![]() |
But, no matter, as we can use the makedirs function. |
![]() |
This will create all the directories in the path if they don’t already exist. |
![]() |
Rmdir allows us to remove directories. So let’s remove towns. |
![]() |
Now let’s try and remove country and its children. |
![]() |
This fails. Rmdir only removes empty directories and is not recursive. |
![]() |
But there is a removedirs function that operates recursively. This removes each directory in the path up to and including the first one mentioned. |
![]() |
So it removes regions. |
![]() |
Then country. |
![]() |
But removedirs only works if the directories are empty. Suppose we had some data files in regions. Then if we call removedirs… |
![]() |
…we’d get an error. Removedirs cannot remove directories that have files. |
![]() |
But rmtree in the shutil module can. |
![]() |
Now that we’ve mentioned it, what about deleting files? Well we could use rmtree on individual files too. But there is the simpler, remove function. So, if we had two files in data and wanted to remove one. |
![]() |
We just use remove. |
![]() |
We can rename and move directories and files too using the rename function. We can rename a directory… |
![]() |
Note that the destination directory must not exist. If it does exist we get an error. |
![]() |
|
![]() |
To keep the same directory name after the rename, we must provide it explicitly. |
![]() |
|
![]() |
Rename also renames files. |
![]() |
|
![]() |
Unlike for directories, a destination file name does not need to be given. |
![]() |
As, in that case, the source file name will be used. |
![]() |
Alternatively, we can rename files and directories using the shutil module’s move function. This is more powerful. Asides from moving the file or directory, it also… |
![]() |
Preserves permission bits, the group and owner. |
![]() |
The last access and modification times. |
![]() |
And other flags. |
![]() |
Renames behaves like both rename and makedirs. |
![]() |
In that it creates any intermediate directories, that do not already exist, like D. |
![]() |
We can copy directories via copytree from shutil. |
![]() |
This copies the entire contents of the directory recursively. Like move, it preserves permission bits, groups and owners, last access and modification times and other flags. |
![]() |
We can copy files via copyfile from shutil. |
![]() |
|
![]() |
Like move and rename for directories… |
![]() |
…a destination file name must always be given |
![]() |
Shutil’s copy function also copies files. |
![]() |
It is like copyfile but we don’t have to provide a destination file name. |
![]() |
It also copies the existing file permissions. |
![]() |
Shutil also has a copy2 function. |
![]() |
This preserves permission bits, groups and owners, last access and modification times and other flags. Copytree, which we saw earlier, uses copy2. |
![]() |
In this episode we have seen how to create, move, copy and delete directories and files using a variety of functions from the os and shutil modules. |
![]() |
Thank you for listening. |


















































