0

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

1 Answers1

0

I have created a file frankenstein.txt with the following content:

frankenstein

The file frankenstein.txt is in the folder C:\Users\richy\OneDrive\Documents\Python Scripts\CIS 521\CIS 521\CIS 521\Homework8.
I have executed the code below:

path = "C:\\Users\\richy\\OneDrive\\Documents\\Python Scripts\\CIS 521\\CIS 521\\CIS 521\\Homework8\\frankenstein.txt"

with open(path, "r") as f:
    print(f.read())

The code prints frankenstein so it finds the file frankenstein.txt.

Obviously I don't have created the file frankenstein.txt in your path, but in a folder inside the documents directory 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 521 in the path?

frankfalse
  • 1,553
  • 1
  • 4
  • 17