How to create a script that list itself ?

Grant Edwards grante at visi.com
Mon Jan 9 14:01:30 EST 2006


On 2006-01-09, Patrick  Allaire <pallaire at gmail.com> wrote:

> How to create a script that list itself ?

This is probably about as close as you're going to get:

  import sys
  pring open(sys.argv[0],'r').read()

And that isn't 100% reliable.
  
> I would like to know, where is the script's code is stored once we
> start it.

It isn't.  At least none of the implimentations I know of have
the script's source code in memory.  The PVM or JVM bytecodes
to which the program has been compiled are in memory somewhere,
and there _may_ be some trick you can use to get at those.

> I know I can achieve that, using files :
>
> print file('myscript.py','rb').read()
>
> But is there a way / a variable that contains the current file in
> memory ?

I don't believe so.

-- 
Grant Edwards                   grante             Yow!  RHAPSODY in Glue!
                                  at               
                               visi.com            



More information about the Python-list mailing list