JohnSmith
JohnSmith

Reputation: 45

Python 3 - reading text from a file

This is exercise 15 from Learn Python the Hard Way, but I'm using Python 3.

from sys import argv
script, filename = argv

txt = open(filename)
print ("Here's your file %r:") % filename
print txt.read()

print ("I'll also ask you to type it again:")
file_again = input()
txt_again = open(file_again)
print txt_again.read()

file is saved as ex15.py, and when I run it from terminal it reads ex15.txt correctly first time, but when I request it second time, I get an error

user@user:~/Desktop/python$ python ex15.py ex15.txt<br>
Here's your file 'ex15.txt':<br>
This is stuff I typed into a file.<br>
It is really cool stuff.<br>
Lots and lots of fun to have in here.<br>

I'll also ask you to type it again:<br>
ex15.txt <b>#now I type this in again, and I get a following error</b><br>
Traceback (most recent call last):<br>
  File "ex15.py", line 11, in <<module>module><br>
    file_again = input()<br>
  File "<<string\>string>", line 1, in <<module>module><br>
NameError: name 'ex15' is not defined

What's wrong?

Upvotes: 3

Views: 20761

Answers (4)

nickanor
nickanor

Reputation: 669

Try this code, for py3k:

txt = open(filename, 'r')
print('Here\'s your file %r: ' % filename)
print(txt.read())
txt.close()

print('I\'ll also ask you to type it again: ')
file_again = input()
txt_again = open(file_again, 'r')
print(txt_again.read())
txt_again.close()

Upvotes: 0

Brendan Long
Brendan Long

Reputation: 54232

You're definitely not using Python 3. There's a couple things that make this obvious:

  • These print statements don't have parenthesis (which are required in Python 3 but not 2):

    print ("Here's your file %r:") % filename
    print txt.read()
    
    print txt_again.read()
    
  • This is calling eval on input() which was changed in Python 3:

    file_again = input()
    

Most likely Python 2 is the default on your system, but you can make your script always use Python 3 by adding this as the first line of your script (if you're running it directly, like ./myscript.py):

#!/usr/bin/env python3

Or running it explicitly with Python 3:

python3 myscript.py

One more note: You should really close the file when you're done with it. You can either do this explicitly:

txt = open(filename)
# do stuff needing text
txt.close()

Or use a with statement and have it handled when the block ends:

with open(filename) as txt:
    # do stuff needing txt
# txt is closed here

Upvotes: 6

Serdalis
Serdalis

Reputation: 10489

You don't seem to be using python 3.0.. to check this, just type into the terminal window:

python

and look at the info lines that appear when the interperator starts.

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel
32
Type "help", "copyright", "credits" or "license" for more information.

It should way something like this,
for python 2.7.3, and for python 3.X.X it would say python 3.X.X instead.

If you are using python 2.X, Ashwini Chaudhary has the correct answer.

Upvotes: 2

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250871

Your print statement suggests you're not using py3k as you said in your title.

print txt.read() this doesn't works in py3k, so make sure you're actually using py3k.

you need to use raw_input() instead of input() because you're on py 2.x.

example py 2.x:

>>> x=raw_input()
foo
>>> x=input()   # doesn't works as it tries to find the variable bar
bar                    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'bar' is not defined

example py 3.x:

>>> x=input()
foo
>>> x       # works fine
'foo'

Upvotes: 4

Related Questions