Search code examples
python-3.xfile-import

Python - Phishing dataset file not being detected even though it exists


I am working on a Machine Learning Project which filters spam/phishing emails out of all emails. For this, I am using the SpamAssassin dataset. The dataset contains different mails in this format:

enter image description here

Now my first task in identifying a phishing/spam email is to find out the no.of web links present in the email. For that, I have written the following code:

wordsInLine = []
tempWord = []
urlList = []


def count():
    flag = 0
    print("Reading all file names in sorted order")
    for filename in sorted(os.listdir("C:/Users/keert/Downloads/Spam_Assassin/spam")):
        file=open(filename)
        count1 = 0
        for line in file:
            
            wordsInLine = line.split(' ')
            for word in wordsInLine:
                
                if re.search('href="http',word,re.I):
                    count1=count1+1

        file.close()
        urlList.append(count1)
        if flag!=0:
            print("File Name = " + filename)
            print ("Number of links = ",count1)
        flag = flag + 1

count()
final = urlList[1:]
print("List of number of links in each email")
print(final)

with open('count_links.csv', 'wb') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    for val in final:
        wr.writerow([val])

print("CSV file generated")

But I'm getting an error saying that the file doesn't exist. The error is : enter image description here

Whereas the file actually exists in the folder. Here's the screenshot: enter image description here

Note: I have also tried to check the file existence using os.path.isfile() function, but that is also returning false. Please, someone, suggest me a solution. Thank you in advance.


Solution

  • It is because you are not reading the file from that directory. os.listdir will only give you a list of file names not an absolute path

    You will have to do something like this to point to the base directory

    base_dir = "C:/Users/keert/Downloads/Spam_Assassin/spam"
    for filename in sorted(os.listdir(base_dir)):
        file=open(os.path.join(base_dir, filename)