473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Jan 9 '06 #1
7 1255
On 2006-01-09, Patrick Allaire <pa******@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
Jan 9 '06 #2
> But is there a way / a variable that contains the current file in
memory ?

yes: import __main__

you can do:

import inspect
import __main__
print inspect.getsource(__main__)

or simply:

print open(__file__).read()
nsz

Jan 9 '06 #3
On 9 Jan 2006 10:09:19 -0800 in comp.lang.python, "Patrick Allaire"
<pa******@gmail.com> wrote:
How to create a script that list itself ?
Stealing from the old C chestnut:

s="s=%c%s%c;print s%%(34,s,34)";print s%(34,s,34)


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 :


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.
Jan 9 '06 #4
########################################

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

Jan 9 '06 #5
Dave Hansen wrote:
Stealing from the old C chestnut:

s="s=%c%s%c;print s%%(34,s,34)";print s%(34,s,34)


Or a bit shorter:

s='s=%s;print s%%`s`';print s%`s`
Jan 10 '06 #6
Duncan Booth <du**********@invalid.invalid> wrote:

Dave Hansen wrote:
Stealing from the old C chestnut:

s="s=%c%s%c;print s%%(34,s,34)";print s%(34,s,34)


Or a bit shorter:

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


It was pointed out to me that the shortest Python program which produces
itself on stdout is:
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jan 12 '06 #7
Tim Roberts <ti**@probo.com> writes:
It was pointed out to me that the shortest Python program which produces
itself on stdout is:
--


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

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jan 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Ideasman | last post by:
Hi I have a made a script that process normals for a flat shaded 3D mesh's. It compares every vert with every other vert to look for verts that can share normals and It takes ages. I'm not...
14
RMWChaos
by: RMWChaos | last post by:
Firebug is reporting "too much recursion" when I attempt to create a child element in a parent that doesn't exist yet. The script should automatically create the missing parent before going on to...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
2
by: santodip | last post by:
I have a dropdown list in ASP.Net page. I want to drag dropdown items up/down from the page view. Can anyone help me out? Thanks in advance
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.