File Not Found Error in Python -


i'm sure has been answered before, couldn't find me...

i'm trying write simple program read file , search word print how many times word found in file. well, every time type in "test.rtf" (which name of document) error...

traceback (most recent call last):   file "/users/ashleystallings/documents/school work/computer programming/side projects/how many? (python).py", line 9, in <module>     filescan= open(filename, 'r')  #opens file filenotfounderror: [errno 2] no such file or directory: 'test.rtf' 

in class last semester, seem remember professor saying have save file in specific place? i'm not sure if said though, i'm running apple osx if helps. haha

here's code, , appreciated :) in advance!

print ("hello! welcome 'how many' program.") filename= input("please enter name of file you'd use.  make \ sure include correct extension!")  #gets file name  filescan= open(filename, 'r')  #opens file  cont = "yes" accumulator = 0  while cont == "yes":     word=input("please enter word scan for.") #asks word     capitalized= word.capitalize()       lowercase= word.lower()      print ("\n")     print ("\n")        #making pretty     print ("searching...")      word in filescan.read():  #checking word            accumulator += 1      print ("the word ", word, "is in file ", accumlator, "times.")      cont = input ('type "yes" check word or \ "no" quit.')  #deciding next step     cont = cont.capitalize()      if cont != "no" or cont != "yes":  #checking valid input         print ("invalid input.")         cont = input ('type "yes" check word or \ "no" quit.')       cont = cont.capitalize()  print ("thanks using how many!")  #ending 

if user not pass full path (on unix type systems means path starting slash) file, path interpreted relatively current working directory. current working directory directory in started program. in case, file test.rtf must in same directory in execute program.

you performing programming tasks in python under mac os. there, recommend work in terminal (on command line), i.e. start terminal, cd directory input file located , start python script there using command

$ python script.py 

in order make work, directory containing python executable must in path, so-called environment variable contains directories automatically used searching executables when enter command. should make use of this, because simplifies daily work greatly. way, can cd directory containing python script file , run it.

in case, if python script file , data input file not in same directory, have specify either relative path between them or have use absolute path 1 of them.


Comments