Why don't isdir() and isfile() work for me?

CLIFFORD ILKAY clifford_ilkay at dinamis.com
Thu Mar 11 05:11:32 UTC 2010


Hi Ray,

A couple of tips to ease your Python journey:

1. Learn how to use pip, virtualenv 
<http://www.saltycrane.com/blog/2009/05/notes-using-pip-and-virtualenv-django/>, 
and virtualenvwrapper 
<http://www.doughellmann.com/projects/virtualenvwrapper/>. You can have 
any number of different versions of Python and modules in their own 
isolated virtual environments that way. You can't always rely on the 
Python packages provided by your distro because first, not every 
potential Python module is packaged by your distro, and second, they may 
lag, and finally, you may need some bleeding edge packages checked 
straight out of svn, git, or hg sometimes.

2. Install and use ipython <http://ipython.scipy.org/moin/>. It's an 
alternative to the standard Python shell and offers some very useful 
features, such as shell completion, history, macros. See below how you 
could have figured out the answer to your question in a matter of 
seconds. (I've read good things about another shell, bpython 
<http://bpython-interpreter.org/> but I haven't used it.)

cilkay at jupiter:~$ ipython
Python 2.6.2 (r262:71600, Jan 25 2010, 18:46:47)
Type "copyright", "credits" or "license" for more information.

IPython 0.10 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import os

# I typed os.is and hit the tab key and got:

In [2]: os.isatty
Out[2]: <built-in function isatty>

# That told me that islink and isfile aren't functions of os. Let's get 
some help.

In [3]: os?
Type:           module
Base Class:     <type 'module'>
String Form:    <module 'os' from 
'/home/cilkay/projects/virtualenvs/ptag_django_cms/lib64/python2.6/os.pyc'>
Namespace:      Interactive
File: 
/home/cilkay/projects/virtualenvs/ptag_django_cms/lib64/python2.6/os.py
Docstring:
     OS routines for Mac, NT, or Posix depending on what system we're 
on.

     This exports:
       - all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc.
       - os.path is one of the modules posixpath, or ntpath
       - os.name is 'posix', 'nt', 'os2', 'ce' or 'riscos'
       - os.curdir is a string representing the current directory ('.' 
or ':')
       - os.pardir is a string representing the parent directory ('..' 
or '::')
       - os.sep is the (or a most common) pathname separator ('/' or ':' 
or '\\')
       - os.extsep is the extension separator ('.' or '/')
       - os.altsep is the alternate pathname separator (None or '/')
       - os.pathsep is the component separator used in $PATH etc
       - os.linesep is the line separator in text files ('\r' or '\n' or 
'\r\n')
       - os.defpath is the default search path for executables
       - os.devnull is the file path of the null device ('/dev/null', etc.)

     Programs that import and use 'os' stand a better chance of being
     portable between different platforms.  Of course, they must then
     only use functions that are defined by all platforms (e.g., unlink
     and opendir), and leave all pathname manipulation to os.path
     (e.g., split and join).


# os.path looks like a good candidate so let's try typing that and "is" 
and hitting tab

In [4]: os.path.is
os.path.isabs    os.path.isdir    os.path.isfile   os.path.islink 
os.path.ismount

# If you want more help:

In [4]: os.path.isdir?
Type:           function
Base Class:     <type 'function'>
String Form:    <function isdir at 0x7f301e3adb18>
Namespace:      Interactive
File: 
/home/cilkay/projects/virtualenvs/ptag_django_cms/lib64/python2.6/genericpath.py
Definition:     os.path.isdir(s)
Docstring:
     Return true if the pathname refers to an existing directory. 



In [5]:

It's quite interactive and productive. You have access to bash from 
within ipython too and when you exit, your ipython history is saved, 
just like bash history.

The Python lists are quite busy, by the way, and irc.freenode.net 
#python is also an excellent resource.
-- 
Regards,

Clifford Ilkay
Dinamis
1419-3266 Yonge St.
Toronto, ON
Canada  M4N 3P6

<http://dinamis.com>
+1 416-410-3326




More information about the ubuntu-users mailing list