-1

i just made the program

j = input("choose a file to read")
l = open(j,"r")
e =l.read()
print e

ran it, and typed in README.txt, which is a real text file located in the same folder as the program, but every time when I type README.txt, i get an error message saying

error FileName: README not defined.

I have python 3.6. Does anyone know what i am doing wrong?

Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
  • You are not actually running that code under `Python 3.6`. The error you are getting is a clear indication that that your `input` is trying to evaluate the input, exactly how Python 2's `input` works. Ensure you are actually in the right Python environment. – idjaw Aug 05 '17 at 14:01
  • Your statement `print e` without parentheses shows that this is not Python 3.6 code, it is Python 2.x, and that explains your problem with the `input` function. Change that to `raw_input()` and see if that solves your problem. If I add parentheses to the `print` line your code works fine in my Python 3.6.2. – Rory Daulton Aug 05 '17 at 14:07
  • But the real solution here, is that if you intend to use Python 3.6, then you need to first make sure your code is actually Python 3 compliant, and you are actually running *for* python 3 and not 2. – idjaw Aug 05 '17 at 14:08

1 Answers1

0

python 3.x has a bit different with python 2.x

first of all you must use print (e) instead of print e (if you use python 3.x )then must close file ( if you don't this it's not raised error but it's important ) so your code must be :

j=input("choose your file  ")


try:
 l=open(j,'r')

 e=l.read()

 print (e)

 l.close()

except:
 print (" no such file ")

also you can use function to do this better :

def readme(j):
 l=open(j,'r')

 e=l.read()

 l.close()

 print (e) # in python 3.x use print () instead of print 

readme("README.txt")
keyvan vafaee
  • 464
  • 4
  • 15