[Edu-sig] Top 5 All Time Novice Obstacles => #3 Where am I ?

Jason Cunliffe Jason Cunliffe" <jasonic@nomadics.org
Fri, 20 Sep 2002 02:44:37 -0400


Title: ["If you don't know where you're going, you might just end up somewhere
else!"]

---Scenario: Novice Pythonista {NP} wants to write a script to generate a list
of files of a certain type. Hopes to grow this into a cgi script to generate
auto-linked directory pages in HTML ---

---{NP thinks:} "hmmm.. Where am I?"

Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help

>>> pwd
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in ?
    pwd
NameError: name 'pwd' is not defined

>>> dir
<built-in function dir>

>>> dir()
['__builtins__', '__doc__', '__name__']

---{NP:} "Yes thank you.. But where am I?"

>>> __name__
'__main__'

---{NP:} [goes off dilignetly and scours docs, books, websites, google and
newgroups to eventually discover magic Python search incantation 'curdir']

>>> curdir
Traceback (most recent call last):
  File "<pyshell#55>", line 1, in ?
    curdir
NameError: name 'curdir' is not defined

---{NP:} [tries IDLE's find to locate 'curdir']

Searching 'curdir' in *.py ...

---{NP:} "This is ridiculous!!"
[NP sensibly now opens google and enters: 'curdir python']

http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=curdir+python

Eyeballing the result quickly #5 on the list look helpful:

Home / Articles / Why I Like Python
... so I can do the same as above: import os, re htmlfiles = filter( lambda
file: re.search("\.html$",
file), os.listdir(os.curdir)) Some expressions in Python are ...
opag.ca/articles/article_mike1.shtml - 26k - 18 Sep 2002 - Cached - Similar
pages
No hits.

>>> os.listdir(os.curdir)
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in ?
    os.listdir(os.curdir)
NameError: name 'os' is not defined

>>> import os
>>> os.curdir
'.'

---{NP:} [laughs at this circular game but encouraged]

>>> os.listdir(os.curdir)
['BACKUP', 'UNWISE.EXE', 'LICENSE.txt', 'README.txt', 'NEWS.txt', 'pycon.ico',
'pyc.ico', 'py.ico', 'python.exe', 'pythonw.exe', 'DLLs', 'w9xpopen.exe',
'libs', 'Lib', 'include', 'tcl', 'Tools', 'Doc', 'INSTALL.LOG',
'PythonCardPrototype-wininst.log', 'RemovePythonCardPrototype.exe',
'PythonCardPrototype', 'bsddb3', 'bsddb3-wininst.log', 'Removebsddb3.exe',
'StandaloneZODB-1.0', 'kviewer.exe', 'Jython_stuff']

---{NP:} "I know I am close. There must be a method in here sonewhere to tell me
where I am. Man, why do they have to make the easy things so HARD???"

>>> dir(os)
['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_RDONLY', 'O_RDWR',
'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO',
'P_OVERLAY', 'P_WAIT', 'R_OK', 'TMP_MAX', 'UserDict', 'W_OK', 'X_OK',
'_Environ', '__all__', '__builtins__', '__doc__', '__file__', '__name__',
'_copy_reg', '_execvpe', '_exists', '_exit', '_get_exports_list',
'_make_stat_result', '_make_statvfs_result', '_notfound', '_pickle_stat_result',
'_pickle_statvfs_result', 'abort', 'access', 'altsep', 'chdir', 'chmod',
'close', 'curdir', 'defpath', 'dup', 'dup2', 'environ', 'error', 'execl',
'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep',
'fdopen', 'fstat', 'getcwd', 'getenv', 'getpid', 'i', 'isatty', 'linesep',
'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir',
'path', 'pathsep', 'pipe', 'popen', 'popen2', 'popen3', 'popen4', 'putenv',
'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'spawnl',
'spawnle', 'spawnv', 'spawnve', 'startfile', 'stat', 'stat_result',
'statvfs_result', 'strerror', 'sys', 'system', 'tempnam', 'times', 'tmpfile',
'tmpnam', 'umask', 'unlink', 'unsetenv', 'utime', 'write']

---{NP:} [tries out some of the 'os' methods until...]

>>> os.getcwd
<built-in function getcwd>

>>> os.getcwd()
'C:\\Python22'

---{NP:} "Bingo!!  Are they kidding - I have to load a frigging 'os' module
every time just to find out where I am?? This can't be right. I must be doing
something wrong."
[ After this small sucess goes back the article
http://opag.ca/articles/article_mike1.shtml ]

for file in os.listdir(os.curdir):
                if file[-5:] == '.html':
                    print "found an html file!"

---{NP:} "hmm ok that looks easy let me try!!

>>> for file in os.listdir(os.curdir):
         print file


BACKUP
UNWISE.EXE
LICENSE.txt
README.txt
NEWS.txt
pycon.ico
etc...

---{NP:} "Great now were cooking. Let's try the current directory:"

>>> for file in os.listdir(os.getcwd):
 print file


Traceback (most recent call last):
  File "<pyshell#5>", line 1, in ?
    for file in os.listdir(os.getcwd):
TypeError: coercing to Unicode: need string or buffer,
builtin_function_or_method found


---{NP:} "Alright, alright..how about this.."


>>> for file in os.listdir(os.getcwd()):
 print file


BACKUP
UNWISE.EXE
LICENSE.txt
README.txt
NEWS.txt
etc...

>>> leodir = 'c:\\program files\\leo\\'
>>> for file in os.listdir(leodir):
         if file[-3:] == '.py':
              print file


leoAtFile.py
leoFrame.py
leoCompare.py
etc...

---{NP:} "Yesss finally:-)

[meanwhile NP's friend opens a REBOL shell on the PC next to him...]

REBOL/View 1.2.8.3.1 3-Aug-2002
>> dir
** Script Error: dir has no value
** Near: dir
>> ? dir
Found these words:
   change-dir      function! Changes the active directory path.
   dir?            function! Returns TRUE if a file or URL is a directory.
   dirize          function! Returns a copy of the path turned into a
directory...
   jdirs           object!   [pm hw ts trees jabber]
   list-dir        function! Prints a multi-column sorted listing of a
director...
   make-dir        function! Creates the specified directory. No error if
alrea...
   what-dir        function! Prints the active directory path
>> what-dir
== %/C/rebol/view/

[..hysterical laughter fills the room..]

Hell-o Novice Pythonistas ?

---------------------------
While you debate the important and finer points of to built_in or not to
built_in, I hope we all think a lot more seriously about what else should be
readily __available__ and __understandable__ to Novices.

IDLE = ['Interactive', 'Development', 'Learning', 'Environment']

./Jason