I am having this problem with any python script that involves opening a text file. I have tried in various IDEs, including VSCode and PyCharm, and all works as intended. But as soon as I run the python script for real it closes (due to an error opening the external file, as discovered by commenting out various sections of code).
Here is a very simple script which runs fine in the IDE but not when actually opening the python file:
main.py:
print("This is a demo of the problem.")
file = open("demofile.txt", "r") #this line causes an error outside of IDE
print(file.readlines())
file.close()
demofile.txt:
this is line 1
this is line 2
this is line 3
Both files are stored in the same folder in Desktop, however when I modify the code to this:
import os
try:
file = open("demofile.txt", "r")
file.close()
except:
print(os.path.abspath("demofile.txt"))
print(os.path.abspath("main.py"))
I get an unexpected output of:
C:\WINDOWS\system32\demofile.txt
C:\WINDOWS\system32\main.py
Any help would be appreciated.