[Tutor] Need help with code
Alan Gauld
alan.gauld at yahoo.co.uk
Wed Apr 19 11:49:26 EDT 2017
On 18/04/17 03:12, Tyler Seacrist wrote:
> How do I avoid this error message everytime I utilize wordlist = open("words.text") ?
>
>>>> wordlist = open("words.txt")
> Traceback (most recent call last):
> File "<pyshell#303>", line 1, in <module>
> wordlist = open("words.txt")
> FileNotFoundError: [Errno 2] No such file or directory: 'words.txt'
You need to tell Python where the file lives. It is evidently not
in the same folder where you run the program(*) so you need to
provide the path.
(*)You can determine which folder python considers its home folder by
including this code in your program;
import os
print( os.getcwd() ) # get Current Working Directory
and you can see its contents with
print( os.listdir() )
So you need to provide the path. If you are using
Windows you should put an 'r' in front of the path,
like so:
fin = open( r"C:\Path\to\words.txt" )
or use forward slashes:
fin = open( "C:/Path/to/words.txt" )
Either technique will avoid python interpreting the '\'
as an escape character.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list