0

For the Code posted below

I have kept a notepad document called nd1.txt in the Folder C:\TempFiles

import os,file,storage
database = file.dictionary()
tools = storage.misc()
lui = -1

def sendWord(wrd, findex):
    global lui
    if findex!=lui:
        tools.refreshRecentList()
        lui = findex
    if tools.mustIgnore(wrd)==0 and tools.toRecentList(wrd)==1:
        database.addWord(wrd,findex)        
def showPostingsList():
    database.display()

def parseFile(nfile, findex):
    for line in nfile:
        pl = line.split()
        for word in pl:
            print(word)
            sendWord(word.lower(),findex)

def parseDirectory():
    files = [open(f) for f in os.listdir('C:\TempFiles')]
    findex = 0
    for nf in files:
        parseFile(nf,findex)
        findex+=1

def main():
    parseDirectory()
    showPostingsList()

main()

Now whenever i'm executing the code, i get the following error msg

Traceback (most recent call last):
  File "E:\Documents\Information Retrieval\postingsList.py", line 39, in <module>
main()
  File "E:\Documents\Information Retrieval\postingsList.py", line 36, in main
parseDirectory(dirname)
  File "E:\Documents\Information Retrieval\postingsList.py", line 28, in parseDirectory
files = [open(f) for f in os.listdir('C:\TempFiles')]
  File "E:\Documents\Information Retrieval\postingsList.py", line 28, in <listcomp>
files = [open(f) for f in os.listdir('C:\TempFiles')]
FileNotFoundError: [Errno 2] No such file or directory: 'nd1.txt'

Even though the file IS there in the mentioned folder, plus every function is working correctly, i've checked with dummy data

Can anyone please tell me where my code went wrong?

4

3 回答 3

1

os.listdir gives you names of the files, not full paths. You need to add the path before calling open, ideally using os.path.join.

于 2013-09-26T15:37:05.800 回答
0

As volferine mentioned, os.listdir only gives names of files.

You'll want to take a look at the os.path.abspath module.

Additionally, it's worthwhile to use some logging code to figure things out -

import os,file,storage
import logging

logging.basicConfig(level=logging.DEBUG)
database = file.dictionary()
tools = storage.misc()
lui = -1

def sendWord(wrd, findex):
    global lui
    if findex!=lui:
        tools.refreshRecentList()
        lui = findex
    if tools.mustIgnore(wrd)==0 and tools.toRecentList(wrd)==1:
        database.addWord(wrd,findex)        
def showPostingsList():
    database.display()

def parseFile(nfile, findex):
    for line in nfile:
        pl = line.split()
        for word in pl:
            print(word)
            sendWord(word.lower(),findex)

def parseDirectory():
    files = [open(f) for f in os.listdir('C:\TempFiles')]
    findex = 0
    logging.debug(os.path.abspath(os.curdir))
    for nf in files:
        logging.debug(os.path.exists(nf))
        parseFile(nf,findex)
        findex+=1

def main():
    parseDirectory()
    showPostingsList()

main()
于 2013-09-26T15:49:59.160 回答
0

Because os.listdir will return the file name not file path. You'd better add C:\TempFiles\ before the file name

于 2013-09-26T15:55:06.530 回答