2

I am doing a small program to help me with learning Python (which I am very new to). I'm using Python 3.2.

In the Python shell, when I enter

f = open('filename.txt', 'r')
f.readlines()

it prints everything in the filename.txt. However, when I type it in a new window and save it with the .py extension, it does not show any output when I run it. It also does not give me any errors.

The code looks somewhat like this:

f = open('filename.txt', 'r')
f.readlines()

while True:
    f = open('filename.txt', 'a')
    inp = input('Enter text: ')
    rest of code...

How do I print everything in the file before going through any of the while statement?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike
  • 69
  • 1
  • 3
  • 9
  • 1
    Please read the formatting guidelines and format your code so that it looks like code. – S.Lott Jan 21 '12 at 21:00
  • "it does not show any output"? Are you using the `print()` function? Or are you just hoping that it will display something. The `>>>` prompt (interactive Python) is special because it magically prints things. Are you hoping for this in a script? Is that what your question is? – S.Lott Jan 21 '12 at 21:03
  • 1
    There's no need to be so harsh on the OP. The behavior they expect is what Matlab and Octave actually do, so it's not *that* strange to expect this. – Fred Foo Jan 21 '12 at 21:05
  • @larsmans: If they expect it, they could consider stating that in the question. It's unclear what -- exactly -- the problem is. That's why I'm asking what the problem actually is. – S.Lott Jan 21 '12 at 23:24
  • thanks for all the help, i will be sure and read formatting guidelines and make sure it is done properly next time. i was expecting it to just magically print was having one of those days ya know – Mike Jan 26 '12 at 23:18
  • I had a similar problem to you, I used the f = open('filename.txt', 'rU') and it worked. The 'rU' needs to be used instead of 'r'. – hello_there_andy Mar 19 '14 at 15:45

3 Answers3

5

however when I type it in a new window and save it with the .py extension

Add a print call. The interactive toplevel prints the value of the last expression entered for convenience, but in a script this would soon get very annoying.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
2

Try this in your code:

print(f.readlines())

The shell evaluates and prints the result of each expression you type, but if you intend to run your program from a file, then you must explicitly print the values that you want to see in the console.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

The Python interpreter runs in two different modes:

  1. The interactive mode, which shows the >>> command prompt and can be accessed by simply typing python or python.exe into the command prompt. This mode has an echo feature which conveniently displays for you the return value of any function or expression you type.

  2. The script mode. When you type into the command line python <yourscript.py>. In this mode, Python hides your script, as well as the return values for the statements you type.

If you want a Python script to display something in the console, use the print() function.

print(f.readlines())

See python.org for more about the Python interpreter.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joel Cornett
  • 24,192
  • 9
  • 66
  • 88