I've been receiving this error for quite some time on a few assignments.
Message=[Errno 2] No such file or directory: 'frankenstein.txt'
Source=C:\Users\richy\OneDrive\Documents\Python Scripts\CIS 521\CIS 521\CIS 521\Homework8\homework8.py
StackTrace:
File "C:\Users\richy\OneDrive\Documents\Python Scripts\CIS 521\CIS 521\CIS 521\Homework8\homework8.py", line 171, in create_ngram_model
with open(path, "r") as f:
File "C:\Users\richy\OneDrive\Documents\Python Scripts\CIS 521\CIS 521\CIS 521\Homework8\Test.py", line 72, in <module>
m = create_ngram_model(1, "frankenstein.txt"); m.random_text(15)
The file is within the solution explorer project and the build action is content. I've found a workaround in which I create string variable of the path but I would like to understand why it cannot locate the file and how to correct this issue.
test_text = os.path.join(os.path.dirname(__file__), 'frankenstein.txt')
I have created a file
frankenstein.txtwith the following content:The file
frankenstein.txtis in the folderC:\Users\richy\OneDrive\Documents\Python Scripts\CIS 521\CIS 521\CIS 521\Homework8.I have executed the code below:
The code prints
frankensteinso it finds the filefrankenstein.txt.Obviously I don't have created the file
frankenstein.txtin your path, but in a folder inside thedocumentsdirectory of my user in my Windows system. In my opinion this difference is not relevant.Pay attention to your path. Are you sure that there are 3 subfolder
CIS 521in the path?