Up: Systems Programming

Querying Directory Contents

slide 001 Hello and welcome to the third episode of the Software Carpentry
lectures on handling directories and files in Python. Here,
we’ll continue to look at how we can explore directories by
looking at the ways in which Python allows us to find out more
about the contents of directories.
slide 002 We now know how to move around directories…
slide 003 …and see what’s in them.
slide 004 But there are many other things we might want to find out.
slide 005 We might want to check whether a file or directory already
exists. This can be useful before saving a file to allow the
user to decide if the file is to be overwritten.
slide 006 We may have a variable and want to see whether that refers to a
file or a directory.
slide 007 We may want to see if two variables refer to the same file or
directory.
slide 008 We may want to find out what we can do with a file or
directory. Are we allowed to read it, to update it, or delete
it?
slide 009 And, we may want to get information such as the file size, who
owns it and when it was last modified.
slide 010 A simple check we often want to do is to see whether a file or
directory exists. Python provides the exists function that does this
check. This takes an argument which can be an absolute or relative
path and returns true if it exists as a file or directory and false
otherwise.
slide 011 Let’s start with a relative path to a file.
slide 012
slide 013 And now an absolute path.
slide 014
slide 015 And a relative path to a directory.
slide 016
slide 017 And the absolute path.
slide 018
slide 019 And something that does not exist.
slide 020
slide 021 And the absolute path to something that does not exist.
slide 022
slide 023 Now, let’s look at telling apart files and directories. Python
provides two functions, isfile and isdir, which check whether
their argument is a path to a file or a directory. They can take
relative or absolute paths. Let’s import these.
slide 024 Now, let’s call isfile on a relative path to a file.
slide 025 As expected, it returns true.
slide 026 And, for a path to a directory.
slide 027 This time, it returns false.
slide 028 And for a path to something that does not exist.
slide 029 It also returns false.
slide 030 Isdir is much the same. When given the path to a directory…
slide 031 …it returns true.
slide 032 And for a file…
slide 033 …it returns false.
slide 034 And when given a nonexistent directory…
slide 035 …again, it returns false.
slide 036 As isfile and isdir are just functions that return true or false
we can use them in conditionals. So here is one example where we
define a simple function to print whether the path it is given
is…
slide 037 A file.
slide 038 A directory.
slide 039 Or does not exist.
slide 040 And here we see it running on a path to a file. This time we use
an absolute path, just for a change.
slide 041
slide 042 And here it is with a path to a directory.
slide 043
slide 044 And with a path something that does not exist.
slide 045
slide 046 Samefile allows us to check whether two paths point to the same
file or directory. This is useful when paths are held in
variables. So, let’s import it.
slide 047 And create some variables with file paths.
slide 048 If we compare file1 and file2, which contain relative and
absolute paths to the same file, then…
slide 049 …we get the expected result of true.
slide 050 And if we compare file1 to a different path, file3, then…
slide 051 …we get false.
slide 052 Before trying to perform operations on a file, for example to
open it for reading or writing, to delete it, or, for files that
are executable binaries, to execute it, it can be useful to
check if we are allowed to do these operations. Python’s access
function allows us to do these checks. So let’s import access.
slide 053 Access takes two arguments, the path to a file or directory and
a flag that specifies what access permissions we want to
check. So let’s import the flags. There are four.
slide 054 As an example of each in turn…. F_OK allows us to check if the
file or directory exists.
slide 055
slide 056 R_OK is for checking if we have permission to read the file or
directory.
slide 057
slide 058 W_OK is for checking if we can edit, update, or delete it.
slide 059
slide 060 And, X_OK is for checking if we can execute a file.
slide 061
slide 062 We can combine conditions using the logical OR, vertical bar,
operator. So we can check if we can both read and write a file.
slide 063
slide 064 Or check if we can both read and execute a file.
slide 065
slide 066 Or if a file exists and we can read it and write it.
slide 067
slide 068 It can also be useful to get operating system information about
files and directories.
slide 069 The stat function returns a record holding various information
about a file.
slide 070 The information in this record can then be accessed. This
includes its protection bits.
slide 071 Inode number.
slide 072 Device.
slide 073 Number of hard links.
slide 074 Owner’s user ID.
slide 075 Owner’s group ID.
slide 076 File size in bytes.
slide 077 Most recent access time. The meaning is operating system
dependant.
slide 078 Most recent modification time. Again, operating system
dependant.
slide 079 The time of the most recent change to metadata, under Linux, or
creation time, under Windows.
slide 080 These times may be floats or integers. You can check this by
calling the stat_float_times function. Here, it says the values
are integers.
slide 081 The stat record may also contain operating system-specific
information.
slide 082 For example, for Linux this can include the number of blocks
used by the file.
slide 083 And the file system block size.
slide 084 We’ve looked at a number of Python functions to find out more
information about files and directories. From the os.path module
we used. Exists to see if a file or directory exists. Isfile and
isdir to determine whether a path specifies a file or a
directory. And, samefile to see whether two paths point to the
same file or directory. From the os module we used. Access to
see what access permissions we have to a file or directory and
determine if we can read it, write to it, delete it, or, for
files, execute it. And, we used stat to get low-level operating
system-specific information such as file sizes, permission bits,
user and group IDs and creation and modification times.
slide 085 Thank you for listening.

  1. Peizhi
    February 15th, 2012 at 11:54 | #1

    Great tutorial! But there seems to be some typos in the slides. I think it should be “from os.path import exists” instead of “from os import exists”. The same is true for isfile and isdir.

  1. No trackbacks yet.