More about simple question

Bengt Richter bokr at oz.net
Tue Nov 5 02:37:11 EST 2002


On Mon, 4 Nov 2002 21:11:52 -0800 (PST), xinghua shi <csshi99 at yahoo.com> wrote:

>--0-1493159574-1036473112=:21756
>Content-Type: text/plain; charset=us-ascii
>
>
>>
>> import sys,os,string 
>> 
>> pid = os.getpid() 
>> 
>> filename = ".ld.mon." + str(pid) 
>> 
>> print filename 
>>
>> ^^^^^^^^^^Name error: name "filename" is not defined. 
>> 
>
>Actually, I have a file named "ld.stat.py" which is: 
>def main():
>
>import sys,os,string 
>pid = os.getpid()  
>filename = ".ld.mon." + str(pid) 
>print filename 
>
>And then I typed:
>
>python ld.stat.py
>
>And I use python2.2. Besides, if I want to run the file "ld.stat.py" by typing "ld.stat" directly, how to do with this? Thanks so much for your help. :)
>

Please post verbatim code in a way that preserves indentation, which is probably
your problem. If you don't know how, say so and don't just ignore advice. Notice:

This is what you claimed to have:

[23:22] C:\pywk\junk>cat ld.stat.py
======================================
def main():

import sys,os,string
pid = os.getpid()
filename = ".ld.mon." + str(pid)
print filename
======================================

This is how you claimed to have run it:

======================================
[23:22] C:\pywk\junk>python ld.stat.py
  File "ld.stat.py", line 3
    import sys,os,string
         ^
IndentationError: expected an indented block
======================================
but you don't mention getting that error, so we correct
part of the indentation, to get the error you did claim
to get:

[23:22] C:\pywk\junk>cat ld.stat.py
======================================
def main():
    import sys,os,string
    pid = os.getpid()
    filename = ".ld.mon." + str(pid)
print filename
======================================

and we run it:
======================================
[23:23] C:\pywk\junk>python ld.stat.py
Traceback (most recent call last):
  File "ld.stat.py", line 5, in ?
    print filename
NameError: name 'filename' is not defined
======================================
Well, that's because main was defined, and filename
was local inside main, and the print filename is outside,
so it can't see inside the main function.

So correct the indentation:

[23:23] C:\pywk\junk>cat ld.stat.py
======================================
def main():
    import sys,os,string
    pid = os.getpid()
    filename = ".ld.mon." + str(pid)
    print filename
======================================

and run it:
======================================
[23:23] C:\pywk\junk>python ld.stat.py

======================================
Nothin came back. That's because all we did
was define main, we never called it. So more changes.

[23:23] C:\pywk\junk>cat ld.stat.py
======================================
def main():
    import sys,os,string
    pid = os.getpid()
    filename = ".ld.mon." + str(pid)
    print filename

if __name__ == '__main__':
    main()
======================================

and we run that:
======================================
[23:24] C:\pywk\junk>python ld.stat.py
.ld.mon.207

======================================
And there you are.

You did not post anything to make it clear what
you were doing. So my advice is to learn to
post verbatim logs of the interactive attempts
you make, otherwise people will think you want
others to do more work guessing than you are
willing to do posting your question, and people
are going to lose interest in responding to your posts.

Regards,
Bengt Richter



More information about the Python-list mailing list