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.

This is a file input exception…you should have to use ‘throws’ and that file I/O Exception and also used to enter path of that file. Here you have 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.

File I/O errors in Python can occur due to a variety of reasons, such as incorrect file path, file not found, incorrect file mode, or permissions issues. Here’s an example of how you can handle file I/O errors in Python:
with open(‘myfile.txt’, ‘r’) as f:
contents =
except FileNotFoundError:
print(“The file does not exist.”)
except PermissionError:
print(“You do not have permission to read the file.”)
except Exception as e:
print(f"An error occurred: {e}")

In this example, we use a try and except block to handle file I/O errors. We attempt to open a file called myfile.txt in read mode using the with statement. If the file exists and we have permission to read it, we read its contents and print them to the console.

However, if any errors occur during the file I/O process, we catch them using the except block. In this example, we catch the FileNotFoundError and PermissionError exceptions to handle cases where the file does not exist or we do not have permission to access it. We also catch any other exceptions using the Exception class to handle unexpected errors. In all cases, we print an appropriate error message to the console to inform the user about the issue.

You can modify this example to suit your specific needs and error handling requirements. Just remember to always use try and except blocks when working with file I/O to avoid crashing your program due to file-related issues.

@yuvraj.khatri107314 @vaishnavi.neela