Have a python file move itself
Question: I'm writing a script that emulates a unix environment with python (yes, I know
that may sound silly).

Basically I set up the "pwd" and "ls" commands before the "cd" command. Now I
need my python script to navigate itself around directories by moving itself.
I was wondering if anyone could point me in the right direction? eg.What
commands to use, etc

Note. I would prefer it if I only had to use built-in modules.

Thank You!


Answer: I am not sure I understand you properly, but if you are talking about moving
through the standard unix file system and not copying the script to different
locations, you could easily just wrap the three commands (pwd,ls,cd) as python
functions, just as you were describing.

In fact, I did this as part of the wx_py project; they aren't perfect clones,
but they work well enough. Here are the three functions:

    
    
    import os
    import glob
    
    def pwd():
        print os.getcwd()
    
    def cd(path,usePrint=True):
        os.chdir(os.path.expandvars(os.path.expanduser(path)))
        if usePrint:
            pwd()
    
    def ls(str='*',fullpath=False):
        g=glob.glob(os.path.expandvars(os.path.expanduser(str)))
        if fullpath:
            for i in g:
                print i
        else:
            for i in g:
                print os.path.split(i)[1]
    

The full file is at:

<https://github.com/davidmashburn/wx_py/blob/master/wx_py/path.py>

I'm sure others may have done a better job, so please just take these
functions as a starting point.

