os.chdir() considered harmful

Guido van Rossum guido at beopen.com
Mon Sep 25 00:09:13 CEST 2000


In a neat little piece of code posted by Tim, I found this gem:

>         # This seems a little-known trick in Python:  changing to the
>         # current directory allows the subsequent isdir-in-a-loop to
>         # work on just the file name, instead of making the OS reparse
>         # the whole darn path from the root again each time.
>         # Depending on the OS, can save gobs of time.

I have no objection to using this in a small self-contained program
like Tim's example.  However I would like to warn that using
os.chdir() is not always a safe practice!  The current directory is
used for several purposes, like interpreting pathnames entered by the
user, and importing Python modules supplied by the user.

If you change the current directory, you may not find the files that
the user asked you to open, or you may find that surprising modules
are imported (the current directory is first in Python's module path
when Python is invoked without a script).

The current directory is also shared between threads, so in a
multi-threaded program, it's doubly scary.

My personal guideline for os.chdir() is to only use it in a main
Python program / script, never in a module imported by others.

Note that every modern Unix uses an effective cache for pathname
parsing, so the speed argument doesn't apply there.  I suspect that it
does apply on Windows though.

I recently heard of an environment where CGI scripts were executed in
multiple threads of the server rather than as subprocesses.  This
requires a relatively small bit of discipline on behalf of the CGI
scripts.  However they were forced to remove os.chdir() because it is
shared between threads...

--Guido van Rossum (home page: http://www.pythonlabs.com/~guido/)




More information about the Python-list mailing list