Find us on GitHub

Teaching basic lab skills
for research computing

Systems Programming: Browsing Directories

Systems Programming/Browsing Directories at YouTube

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.

We have seen how Python allows us to…

…save data into files.

And, read data from files.

But we might also want to do a number of other things. For example…

…see what files we have. For example if offering a choice of files for users to select.

We might wish to delete files.

Group files into directories.

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.

Now, we could use the shell to do these directory and file manipulations…

…but then we would need both our Python program…

… and our shell commands.

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.

Fortunately, we can do it all in Python.

As a simple example we'll start by accessing the current directory. First, we import the getcwd function from the os module.

If we then run getcwd…

…Python will show us the current working directory. That is, the directory in which Python was started.

getcwd is a function and it returns the current directory, so we can assign that to a variable.

Then we can use that variable.

For example, print it.

To list the contents of a directory we can use the listdir function. So let us import that.

Listdir takes one argument, the path of the directory to list. This can be relative or absolute.

So let us use a dot to state that we want to list the current working directory.

Listdir returns a list of the contents of the current directory.

Note that the file and directories in the list are not in alphabetical order.

Instead of using the dot convention for the current directory, it's cleaner, and more portable, if…

…we use getcwd.

As we can see, the result is the same.

And, here we use listdir but with the originaldir variable in which we stored the value of getcwd.

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.

We can then print files.

And voila.

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.

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.

Then we just press return on a blank line. The loop is now finished and will execute.

And, as we can see, each member of files is printed.

To change the current working directory we use the chdir function. So let us import that.

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.

We can use getcwd to check that we have indeed changed the working directory.

Good.

And then we can use listdir to see the what's in data.

That's correct.

As we saved our original directory in the variable originaldir we can use this with chdir to get back to where we came from.

And use getcwd to show that this has indeed happened.

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.

Using getcwd we can see that…

…as far as Python is concerned we are now in users vlad data.

If we exit Python using control-D.

And run the Linux pwd command.

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.

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.

Thank you for listening.