Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting a File not Found error on a simple Python open() function

Here is the code I have boiled it down to a simple open(), the Open file input statement displays but the Close file does not. This runs in the Idle interface but not in the command line interface.

Both the program and the file (spelled correctly and all lower case) are on the desktop for this test. Does anyone see what is missing?open

# Read It
# Demonstrates reading from a text file

input("\n\nPress the enter key to Open file")
print("Opening and closing the file.")
text_file = open("nicole1.txt", "r")
input("\n\nPress the enter key to Close file")
text_file.close()

input("\n\nPress the enter key to exit.")

** Update, Ok I tried the absolute path and it was not successful. I have a copy of this on a flash drive. I ran it on a Windows XP box and a Windows 7 box and it ran just fine. I take the same flash drive and try and run it on a Windows10 Box and I get the problem. One comment asked if there was a traceback and there is and it basically indicates that the file does not exist. I am now trying to determine if this is a Windows 10 issue. Also, the code will run inside idle on both Windows Boxes (XP and Win10).

like image 495
Nicole Cook Avatar asked Nov 24 '15 01:11

Nicole Cook


People also ask

How do I fix file Not Found error in Python?

Since Python can not find the file, we are opening it creates an exception that is the FileNotFoundError exception. In this example, the open() function creates the error. To solve this error, use the try block just before the line, which involves the open() function: filename = 'John.

Why is Python saying no such file or directory?

The Python "FileNotFoundError: [Errno 2] No such file or directory" occurs when we try to open a file that doesn't exist in the specified location. To solve the error, move the file to the directory where the Python script is located if using a local path, or use an absolute path.

What is file Not Found error?

FileNotFoundException is a checked exception in Java that occurs when an attempt to open a file denoted by a specified pathname fails. This exception is thrown by the FileInputStream , FileOutputStream , and RandomAccessFile constructors when a file with the specified pathname either does not exist or is inaccessible.


2 Answers

I had the same problem on Windows 10 and the solution was, as stupid as it sounds, to simply decrease the path length. Apparently, Windows (10) still has problems with very long paths in 2021 AD.

like image 53
Hagbard Avatar answered Nov 15 '22 09:11

Hagbard


Well, besides the fact that I named it nicole1.txt.txt by accident initially (due to the way that Windows automatically uses extensions and Linux does not), it works perfectly fine for me using Windows 10 Home and Pro, and Ubuntu 16.04. I simply just executed python test.py in the command prompt with test.py containing your script and making sure both files were in the same directory.

I cannot test it on any other Windows version, as I no longer use them.

Also, btw, you may want to rewrite your code to:

input("\n\nPress the enter key to Open file")
print("Opening and closing the file.")
with open("nicole1.txt", "r") as text_file:
    input("\n\nPress the enter key to Close file")
    text_file.close()
input("\n\nPress the enter key to exit.")

This way, you make sure the file is closed no matter what happens. And yes, I know, technically speaking the file is closed twice.

like image 28
1313e Avatar answered Nov 15 '22 09:11

1313e