Up: Systems Programming

Browsing Directories

slide 001 Hello and welcome to the first episode of the Software Carpentry
lectures on handling directories and files in Python. In this
episode, we’ll have a look at how directories can be browsed in
Python.
slide 002 We have seen how Python allows us to…
slide 003 …save data into files.
slide 004 And, read data from files.
slide 005 But we might also want to do a number of other things. For
example…
slide 006 …see what files we have. For example if offering a choice of
files for users to select.
slide 007 We might wish to delete files.
slide 008 Group files into directories.
slide 009 And, structure these directories into a tree. For example we
might want to create a directory containing a set of input files
holding data for processing.
slide 010 Now, we could use the shell to do these directory and file
manipulations…
slide 011 …but then we would need both our Python program…
slide 012 … and our shell commands.
slide 013 Our program would not be very portable. If we wanted to run it
on both Linux and Windows we’d need two sets of shell commands.
slide 014 Fortunately, we can do it all in Python.
slide 015 As a simple example we’ll start by accessing the current
directory. First, we import the getcwd function from the os
module.
slide 016 If we then run getcwd…
slide 017 …Python will show us the current working directory. That is,
the directory in which Python was started.
slide 018 getcwd is a function and it returns the current directory, so we
can assign that to a variable.
slide 019 Then we can use that variable.
slide 020 For example, print it.
slide 021 To list the contents of a directory we can use the listdir
function. So let us import that.
slide 022 Listdir takes one argument, the path of the directory to
list. This can be relative or absolute.
slide 023 So let us use a dot to state that we want to list the current
working directory.
slide 024 Listdir returns a list of the contents of the current directory.
slide 025 Note that the file and directories in the list are not in
alphabetical order.
slide 026 Instead of using the dot convention for the current directory,
it’s cleaner, and more portable, if…
slide 027 …we use getcwd.
slide 028 As we can see, the result is the same.
slide 029 And, here we use listdir but with the originaldir variable in
which we stored the value of getcwd.
slide 031 As for getcwd, we can save the output of listdir in a
variable. So, we can list the directory whose name is in
originaldir and save this list in a variable called files. Files
will now contain a list of the files and directories in
originaldir.
slide 032 We can then print files.
slide 034 We can use a for-in loop to print each file in the list in
turn. So, here we say we want to consider each element of files
in turn. Remember the colon.
slide 035 We then provide our loop body. Here, we will just print the
variable, file. Remember that we need to type in four spaces
before print.
slide 036 Then we just press return on a blank line. The loop is now
finished and will execute.
slide 037 And, as we can see, each member of files is printed.
slide 038 To change the current working directory we use the chdir
function. So let us import that.
slide 039 This function takes one argument, the path of the directory to
be the new current working directory. This can be an absolute or
a relative path. Let’s run it to change into the data directory.
slide 040 We can use getcwd to check that we have indeed changed the
working directory.
slide 042 And then we can use listdir to see the what’s in data.
slide 044 As we saved our original directory in the variable originaldir
we can use this with chdir to get back to where we came from.
slide 045 And use getcwd to show that this has indeed happened.
slide 047 When we call chdir in Python it only changes the current working
directory as known to Python. What the shell, within which we
started Python, considers to be the current working directory
remains unchanged. For example, let us again change into the
data directory.
slide 048 Using getcwd we can see that…
slide 049 …as far as Python is concerned we are now in users vlad data.
slide 050 If we exit Python using control-D.
slide 051 And run the Linux pwd command.
slide 052 We see that as far as the shell is concerned we are still in
users vlad, which is the directory in which we started Python.
slide 053 To summarize, we used the following functions in the Python os
module to browse directories. Getcwd allows us to get the
current working directory. Listdir allows us to list the
contents of a specific directory. And, chdir allows us to change
the current working directory to be a specific directory.
slide 054 Thank you for listening.

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