Up: Systems Programming

Manipulating Files and Directories

slide 001 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.
slide 002 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.
slide 003
slide 004 We can use listdir to see that we indeed have a new directory
called data.
slide 005 And, that this new directory is empty.
slide 006 Now, if we try to use mkdir again…
slide 007 …we get an error since it already exists.
slide 008 Suppose we want to make a nested set of directories for a more
complex organisation of files. Let’s give that a try.
slide 009 It fails.
slide 010 But, no matter, as we can use the makedirs function.
slide 011 This will create all the directories in the path if they don’t
already exist.
slide 012 Rmdir allows us to remove directories. So let’s remove towns.
slide 014 Now let’s try and remove country and its children.
slide 015 This fails. Rmdir only removes empty directories and is not
recursive.
slide 016 But there is a removedirs function that operates
recursively. This removes each directory in the path up to and
including the first one mentioned.
slide 017 So it removes regions.
slide 018 Then country.
slide 019 But removedirs only works if the directories are empty. Suppose
we had some data files in regions. Then if we call removedirs…
slide 020 …we’d get an error. Removedirs cannot remove directories that
have files.
slide 021 But rmtree in the shutil module can.
slide 023 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.
slide 024 We just use remove.
slide 025 We can rename and move directories and files too using the
rename function. We can rename a directory…
slide 027 Note that the destination directory must not exist. If it does
exist we get an error.
slide 028
slide 029 To keep the same directory name after the rename, we must
provide it explicitly.
slide 030
slide 031 Rename also renames files.
slide 032
slide 033 Unlike for directories, a destination file name does not need to
be given.
slide 034 As, in that case, the source file name will be used.
slide 035 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…
slide 036 Preserves permission bits, the group and owner.
slide 037 The last access and modification times.
slide 038 And other flags.
slide 039 Renames behaves like both rename and makedirs.
slide 040 In that it creates any intermediate directories, that do not
already exist, like D.
slide 041 We can copy directories via copytree from shutil.
slide 042 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.
slide 043 We can copy files via copyfile from shutil.
slide 044
slide 045 Like move and rename for directories…
slide 046 …a destination file name must always be given
slide 047 Shutil’s copy function also copies files.
slide 048 It is like copyfile but we don’t have to provide a destination
file name.
slide 049 It also copies the existing file permissions.
slide 050 Shutil also has a copy2 function.
slide 051 This preserves permission bits, groups and owners, last access
and modification times and other flags. Copytree, which we saw
earlier, uses copy2.
slide 052 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.
slide 053 Thank you for listening.

  1. No comments yet.
  1. No trackbacks yet.