[Tutor] Newbie question. [primitive operations / scheme tangent]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 26 May 2002 23:11:40 -0700 (PDT)


On Mon, 27 May 2002, Pijus Virketis wrote:

> >I'm trying to use the os.listdir module to list a directory and then
> >store
> >that output to a list. I'm probably doing this wrong:
> >X = listdir(/)
>
> A quick note: I assume that you have imported the os module with "from os import
> *", seeing that you don't need to qualify the call to listdir(). I suggest that
> you instead do:
>
> >>> import os
> >>> path_list = os.listdir("/") # note the quotes!
>
>
>  So, the problem was that the listdir() function was expecting a string
> argument, i.e. "/" rather than just / that you passed to it.

Hi SA, hope things are going well!


Another reason why:

    x = listdir(/)

doesn't quite work is that '/' is the symbol for division.  Just as '+'
and '-' stand for addition and subtraction, Python uses '/' for division.


Let's see what happens with Python when we try putting those mathy
operations standalone in an interactive interpreter:

###
>>> +
  File "<stdin>", line 1
    +
    ^
SyntaxError: invalid syntax
>>> /
  File "<stdin>", line 1
    /
    ^
SyntaxError: invalid syntax
###



In these cases, Python flags each as a "syntax error" because it's
assuming that we've forgotten to say what to do WHICH by WHAT.  These are
formally called "binary" operators because they always need to have
something on the left and right sides of them for Python to make sense out
of them:

###
>>> 14 / 2
7
###



and if we leave the left and right sides out, we get a syntax error.
Let's see what happens when we try to say 'x = listdir(/)':

###
>>> x = listdir(/)
  File "<stdin>", line 1
    x = listdir(/)
                ^
SyntaxError: invalid syntax
###

So the interpreter actually zeroes in on the division symbol: it just
looks so weird to Python that it just points its finger right on it!
*grin* If you try the interactive interpreter on suspicious lines, you may
find it useful while you're trying out new things.  It's not perfect, but
it can be helpful.



[Random, offtopic note: this situation is different from the Scheme
programming language, where even '+ and '- can be used standalone.  For
example, it's possible to say something like this in Scheme:

;;;
dyoo@coffeetable:~$ guile
guile> +
#<primitive-procedure +>
guile> (define (apply-with-42-and-24 function)
  (function 42 24))
guile> (apply-with-42-and-24 +)
66
guile> (apply-with-42-and-24 -)
18
guile> (apply-with-42-24 /)
1.75
;;;

Sorry, I just had to go off-tangent for a moment.  *grin*]



Anyway, good luck!  If you have more questions, please feel free to ask!