2 things regarding interactive interpreter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andu
    New Member
    • Apr 2007
    • 8

    2 things regarding interactive interpreter

    First off, i apologize for the badly named thread... I tried to edit the name, but for some reason the new name won't update...


    Ok, so i'm new to python.. but i've undertaken a project in python, and i have to see it through...

    So far i'm having two persistant problems...

    1. If i have a python script, called for example prog.py, if i try running it in the terminal, or in DOS as 'python prog.py' it runs perfectly (well, in most cases). However if i run 'prog.py' in a python shell, i get the following error

    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in <module>
    NameError: name 'prog' is not defined


    My code is

    #!C:/Python25
    #Filename prog.py

    fileobj=open('C :/Python25/user progs/testtext.txt',' r')
    fileobj.read()



    2. Regarding the fileobj.read()
    If i type the two commands
    ie: fileobj=open('C :/Python25/user progs/testtext.txt',' r')
    fileobj.read()
    separately in the shell, i get the expected output. However, when i run the script in the command terminal, (as above: 'python prog.py' ) it does not give me any errors, however it does not display the contents of the file....



    Can anyone lead me in the right direction please?
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by Andu
    Ok, so i'm new to python.. but i've undertaken a project in python, and i have to see it through...

    So far i'm having two persistant problems...

    1. If i have a python script, called for example prog.py, if i try running it in the terminal, or in DOS as 'python prog.py' it runs perfectly (well, in most cases). However if i run 'prog.py' in a python shell, i get the following error

    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in <module>
    NameError: name 'prog' is not defined


    My code is

    #!C:/Python25
    #Filename prog.py

    fileobj=open('C :/Python25/user progs/testtext.txt',' r')
    fileobj.read()



    2. Regarding the fileobj.read()
    If i type the two commands
    ie: fileobj=open('C :/Python25/user progs/testtext.txt',' r')
    fileobj.read()
    separately in the shell, i get the expected output. However, when i run the script in the command terminal, (as above: 'python prog.py' ) it does not give me any errors, however it does not display the contents of the file....



    Can anyone lead me in the right direction please?
    The reason it is not printing the contents of the file is because you didn't specifically said "print". In the interactive prompt "print" is not necessary but if you want to print something when running from a file you have to specificley
    write "print" like:
    Code:
    print fileobj.read()
    or
    Code:
    text = fileobj.read()
    print text
    Does that help?

    Comment

    • Andu
      New Member
      • Apr 2007
      • 8

      #3
      Yes, thanks that solved one problem

      ok, i understood why it will not work in the shell...

      it's like typing '8+7' in the shell working as opposed to the command terminal....

      Thanks :)

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by Andu
        First off, i apologize for the badly named thread... I tried to edit the name, but for some reason the new name won't update...
        No problem, I take care of these things.
        BTW, welcome to the Python Forum on TheScripts.com.
        Ok, so i'm new to python.. but i've undertaken a project in python, and i have to see it through...

        So far i'm having two persistant problems...

        1. If i have a python script, called for example prog.py, if i try running it in the terminal, or in DOS as 'python prog.py' it runs perfectly (well, in most cases). However if i run 'prog.py' in a python shell, i get the following error

        Traceback (most recent call last):
        File "<interacti ve input>", line 1, in <module>
        NameError: name 'prog' is not defined


        My code is

        Code:
        #!C:/Python25
        #Filename prog.py
        
        fileobj=open('C:/Python25/user progs/testtext.txt','r')
        fileobj.read()
        Here, I've changed INDENT tags to CODE tags.
        To run a script form the Python shell:
        Code:
        import prog
        Leave off the '.py'
        See this thread regarding the search path for imports.
        2. Regarding the fileobj.read()
        If i type the two commands
        ie: fileobj=open('C :/Python25/user progs/testtext.txt',' r')
        fileobj.read()
        separately in the shell, i get the expected output. However, when i run the script in the command terminal, (as above: 'python prog.py' ) it does not give me any errors, however it does not display the contents of the file....

        Can anyone lead me in the right direction please?
        Look's like you got this one...

        Comment

        • Andu
          New Member
          • Apr 2007
          • 8

          #5
          Ah, ok, I tired it that way and it worked.
          Thanks loads for your help :)

          Comment

          Working...