ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
[Tutor] using cmd
by Christopher Spears other posts by this author
Mar 17 2006 3:37PM messages near this date
Re: [Tutor] Fill in a web form | Re: [Tutor] using cmd
I just completed an assignment out of Learning Python
in which I used the Cmd class from the cmd module to
create a little shell:

import cmd, os, shutil, sys

class shell(cmd.Cmd):
    def do_EOF(self, line):
        sys.exit()
        
    def do_ls(self, line):
        if line == '': dirs = [os.curdir]
        else: dirs = line.split()
        for dirname in dirs:
            print 'Listing of %s:' % dirname
            print '\n'.join(os.listdir(dirname))

    def do_cd(self, path):
        os.chdir(path)

    def do_mv(self, line):
        src, dest = line.splt()
        os.rename(src, dest)

    def do_cp(self, line):
        words = line.split()
        sourcefiles, target = words[:-1], words[-1]
        for sourcefile in sourcefiles:
            shutil.copyfile(sourcefile, target)

    def do_rm(self, line):
        [os.remove(arg) for arg in line.split()]

class DirectoryPrompt:
    def __repr__(self):
        return os.getcwd() + '>  '

cmd.Cmd.prompt = DirectoryPrompt()
newShell = shell()
newShell.cmdloop()


My main question concerns the naming of functions such
as:

def do_ls(self, line):
        if line == '': dirs = [os.curdir]
        else: dirs = line.split()
        for dirname in dirs:
            print 'Listing of %s:' % dirname
            print '\n'.join(os.listdir(dirname))

Originally, I called the function 'ls', but when I did
that, the function didn't work when I typed 'ls' at
the prompt.  When I looked in the back of the book, I
saw the name the authors gave their function, which
was 'do_ls'.  When I changed the function's name to
do_ls, the function worked when I typed 'ls' at the
prompt.  Does anyone know why this happened?

  


_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Christopher Spears
Kent Johnson
Alan Gauld

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved