8

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).

Nicole Cook
  • 316
  • 1
  • 5
  • 11
  • 2
    Try with absolute path to file, if that solves the problem then, well, there is your problem! – Tymoteusz Paul Nov 24 '15 at 01:24
  • 3
    Do you get a traceback? First guess is that you are running the script from a different directory. – Paul Rooney Nov 24 '15 at 01:24
  • 8
    Before you `open()` the file, put `import os` and `print(os.path.abspath('nicole1.txt'))`. I bet it doesn't print `C:\Users\Nicole\Desktop\nicole1.txt`. – TigerhawkT3 Nov 24 '15 at 01:27
  • 7
    Your program and file are on the desktop, but that doesn't mean that your current working directory is also there. You can test that with `print(os.getcwd())`. When a text file is in the same directory as a python module, a common way to get its name is `os.path.join(os.path.abspath(__file__), "nicole1.txt")`. – tdelaney Nov 24 '15 at 02:30
  • 1
    About 1% of my students report similar errors when they try to open text files in Windows 10 for reading. In most cases, the file is being opened perfectly, but sometimes (every fifth time perhaps) the same program when being run in Idle shows a "file not found error". Terminating Idle and restarting the program often helps. – Marvo Feb 23 '21 at 16:02
  • 1
    In addition to @Marvo's comment: How are you executing your script on the Windows 10 machine? If you use an IDE with built-in terminal (VS Code, PyCharm ...) that might be a problem related to the working directory (as mentioned by others). – albert Mar 19 '21 at 21:24
  • @albert as you said in my case it happened because of VSCode default directory. – Alexis Apr 20 '21 at 16:54

3 Answers3

3

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.

Hagbard
  • 3,430
  • 5
  • 28
  • 64
  • 1
    Further info about path length: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation and https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation – albert Mar 19 '21 at 21:20
  • I thought this might be my problem, though my filepath was 224 characters long, smaller than the 260 character limit mentioned. In case it helps someone, I tried this command on the PowerShell as administrator (from Microsoft's doc): New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" ` -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force – Joshua M Oct 14 '22 at 22:43
1

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.

1313e
  • 1,112
  • 9
  • 17
-2

How are you running this program? If by command line, then put the file into same folder where the python file that contains this code is situated. As others have pointed out, the issue is program is unable to find the file. If you're running this via some IDE, then set working directory to this path. For example, Pycharm (and other Intellij IDEs) have this in "Edit Configuration".

Jazib
  • 1,200
  • 1
  • 16
  • 39