Error in a file i/o python program

Hello, @yuvraj.khatri107314
Apologies for the late response.
First of all you have to make sure your indentations are correctly placed,
then you have to use “a” instead of “r” in the code ,
r : read only (if there is a file existing)
w: Write(to create a file if it doesn’t exists)
a: Append(to create a file and also for appending new data to existing file)

Some references : Python - Files I/O

Hope this solves your problem.

Feel free to contact us, Thanks for being patient

Lokesh Agarwal

The code is not indented properly. because the print statement should be indented to the right once. And we have to use “a” instead of “r” in the brackets.

Your code has no syntax errors. As you want to print the data you read, ‘r’ mode is sufficient. Looking at the error you’re incurring, I think either your text file is non-existent or the current directory you are in is not where you have stored the file. Please check the location of the file you are trying to open.

Hi @yuvraj.khatri107314
FileNotFoundError shows due to an extension error or wrong file path specified or the file may not present in the same directory i.e. your working directory.

Hope, it’ll solve your problem.

your code is showing error regarding file you are trying to open.
Check the file ‘textsample.txt’ is in your current directory or not if it is not add to path.
If you want to write new file / you are ready to create new file of same name you can use ‘w’,‘wb’, ‘a’ instead of ‘r’.
Hope you got your solution
Feel free to contact.

You Are Working In Vs Code Some Times In Vs Code You Should Delete Your Terminal And Again Run The Code That Will Work Perfectly

The Code is absolutely correct but while opening the input function you want to use ‘a’ instead of ‘r’ ‘a’ is used to append a file into the program while ‘r’ is used to read the file in the program
Hope this will solve your problem

This is fileinput exception …you should have to use an throws and that file I/O Exception and also used to enter path of that file…

When you open a file with the name “filename.ext”; you are telling the open() function that your file is in the current working directory . This is called a relative path.

file = open('filename.ext') //relative path

In the above code, you are not giving the full path to a file to the open() function, just its name - a relative path. The error “FileNotFoundError: [Errno 2] No such file or directory” is telling you that there is no file of that name in the working directory. So, try using the exact, or absolute path.

file = open(r'C:\path\to\your\filename.ext') //absolute path

In the above code, all of the information needed to locate the file is contained in the path string - absolute path.

If the user does not pass the full path to the file (on Unix type systems this means a path that starts with a slash), the python file path is interpreted relatively to the current working directory. The current working directory usually is the directory in which you started the program. In order to make this work, the directory containing the python executable must be in the PATH, a so-called environment variable that contains directories that are automatically used for searching executables when you enter a command. In any case, if your Python script file and your data input file are not in the same directory, you always have to specify either a relative path between them or you have to use an absolute path for one of them.