0

I'm trying to open a file using this code but it is again and again showing the error: NameError: name "practice" is not defined where "practice" is the name of the file which I'm trying to open. "practice.txt" (the file that im trying to open) is present in the same directory in which my python program has been created.

import os name = input("Enter the name of the file : ") if(os.path.exist(name)==True): fopen = open(name,'w') line = fopen.readline() while(line != ""): print(line) line=fopen.readline() fopen.close() else: print("file does not exist!!!") 
4
  • Is that Python 2? Commented Oct 12, 2017 at 14:24
  • Based on your error message, you should be using raw_input Commented Oct 12, 2017 at 14:24
  • python 2: input should be raw_input Commented Oct 12, 2017 at 14:24
  • You are using Python 2. Use raw_input instead of input. Commented Oct 12, 2017 at 14:26

1 Answer 1

0

This is because you must specify the PATH of the file as well as the filename.

I.E. open("Files/images/Image1.png", "MODE")

Sign up to request clarification or add additional context in comments.

4 Comments

If the file is in the same directory, the path is not needed
True, @Wondercricket, but you need the file extension as well. Maybe that's what he forgot.
I didn't forget the extension. I am writing the file name alongwith the extension ".txt" @EgMusic
@AnantVikramSingh oh, okay.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.