How to create a script that list itself ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick  Allaire

    #1

    How to create a script that list itself ?

    How to create a script that list itself ?

    I would like to know, where is the script's code is stored once we
    start it. 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 ?

    Thanks.

    Xaqc

  • Grant Edwards

    #2
    Re: How to create a script that list itself ?

    On 2006-01-09, Patrick Allaire <pallaire@gmail .com> wrote:
    [color=blue]
    > How to create a script that list itself ?[/color]

    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.
    [color=blue]
    > I would like to know, where is the script's code is stored once we
    > start it.[/color]

    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.
    [color=blue]
    > 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 ?[/color]

    I don't believe so.

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

    Comment

    • Szabolcs Nagy

      #3
      Re: How to create a script that list itself ?

      > But is there a way / a variable that contains the current file in[color=blue]
      > memory ?[/color]
      yes: import __main__

      you can do:

      import inspect
      import __main__
      print inspect.getsour ce(__main__)

      or simply:

      print open(__file__). read()


      nsz

      Comment

      • Dave Hansen

        #4
        Re: How to create a script that list itself ?

        On 9 Jan 2006 10:09:19 -0800 in comp.lang.pytho n, "Patrick Allaire"
        <pallaire@gmail .com> wrote:
        [color=blue]
        >How to create a script that list itself ?[/color]

        Stealing from the old C chestnut:

        s="s=%c%s%c;pri nt s%%(34,s,34)";p rint s%(34,s,34)

        [color=blue]
        >
        >I would like to know, where is the script's code is stored once we
        >start it. I know I can achieve that, using files :[/color]

        Well, in the above, the script (or rather, the information necessary
        to print the script) is actually stored in a string that is part of
        the script...

        Regards,
        -=Dave

        --
        Change is inevitable, progress is not.

        Comment

        • calfdog@yahoo.com

          #5
          Re: How to create a script that list itself ?

          ############### ############### ##########

          import sys

          path = os.path.dirname (sys.argv[0])
          print "Path:%s" % (path)



          ############### ############### ########

          If you ran this as a script,
          This would print the location of where the script itself is running.



          Hope it helps!

          Rob

          Comment

          • Duncan Booth

            #6
            Re: How to create a script that list itself ?

            Dave Hansen wrote:
            [color=blue]
            > Stealing from the old C chestnut:
            >
            > s="s=%c%s%c;pri nt s%%(34,s,34)";p rint s%(34,s,34)[/color]

            Or a bit shorter:

            s='s=%s;print s%%`s`';print s%`s`

            Comment

            • Tim Roberts

              #7
              Re: How to create a script that list itself ?

              Duncan Booth <duncan.booth@i nvalid.invalid> wrote:[color=blue]
              >
              >Dave Hansen wrote:
              >[color=green]
              >> Stealing from the old C chestnut:
              >>
              >> s="s=%c%s%c;pri nt s%%(34,s,34)";p rint s%(34,s,34)[/color]
              >
              >Or a bit shorter:
              >
              >s='s=%s;prin t s%%`s`';print s%`s`[/color]

              It was pointed out to me that the shortest Python program which produces
              itself on stdout is:
              --
              - Tim Roberts, timr@probo.com
              Providenza & Boekelheide, Inc.

              Comment

              • Mike Meyer

                #8
                Re: How to create a script that list itself ?

                Tim Roberts <timr@probo.com > writes:[color=blue]
                > It was pointed out to me that the shortest Python program which produces
                > itself on stdout is:
                > --[/color]

                Which, oddly enough, is also the shortest shell program that produces
                itself on stdout.

                <mike
                --
                Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                Comment

                Working...