How do I run a Python script in the command line with an embedded text file?
Question: there. I apologize for the simple question, but I am a complete novice and
need some help. I am trying to run Peter Norvig's Spelling Corrector
(http://norvig.com/spell.py), but I'm receiving the following reply:

    
    
    C:\>spelling.py
    Traceback (most recent call last):
      File "C:\Python27\spelling.py", line 11, in <module>
        NWORDS = train(words(file('big.txt').read()))
    IOError: [Errno 2] No such file or directory: 'big.txt'
    

The script includes an embedded text file (big.txt), which I've created and
saved in the same directory as spelling.py. Why can't it find the big.txt
file? Secondly, once the script is working, how would I use it against a
sample of words needing correction?


Answer: The working directory of your script is the current folder, not the script's
folder.

You can add the following code to your script to change this:

    
    
    import os
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    

Of course you could also use this folder only when opening the file instead of
changing the working directory:

    
    
    path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'big.txt')
    NWORDS = train(words(file(path).read()))
    

