Search code examples
pythontext-filespython-3.8

Why is it giving No file directory found?


It is giving me FileNotFoundError: [Errno 2] No such file or directory: 'filepath' error. While I have defined the function "filepath" and called it in proper location. When I run the code the button appears and after that, when I assign it the file in which the script should operate, it shows me the above error.

#from typing_extensions import Counter
from tkinter import *
from tkinter import filedialog
import csv

def openFile():
    filepath = filedialog.askopenfilename()
    acronyms = 0  # number of acronyms
    acronym_word = []  # creating a list to store acronyms found in the file
    with open('filepath', "r", errors='ignore') as file:
        text = str(file.read())
        for word in text.split(' '):  # for every word in line
            if word.isupper() and word.isalpha():  # if word is all uppercase letters
                acronyms += 1
                if len(word) == 1:  # ignoring the word found in the file of single character as they are not acronyms
                    pass
                else:
                    index = len(acronym_word)
                    acronym_word.insert(index, word)  # storing all the acronyms founded in the file to a list

    uniqWords = sorted(set(acronym_word))  # remove duplicate words and sort the list of acronyms
    for word in uniqWords:
        print(word, ":",
              acronym_word.count(word))  # printing the all the acronyms along with no of times they appear in the file
        with open('LOG.csv', 'w', newline='') as csvfile:  # creating the csv file

            fieldnames = ['Name_of_acronyms', 'Number_of_times_they_occured']

            thewriter = csv.DictWriter(csvfile, fieldnames=fieldnames)

            thewriter.writeheader()

            for word in uniqWords:
                thewriter.writerow({'Name_of_acronyms': word,
                                    'Number_of_times_they_occured': acronym_word.count(word)})  # Extrating the CSV file
                full_acronym = []  # creating a list of acronym for which we will search its full form occurence in the document

    for word in uniqWords:
        words = str(
            "(" + word + ")")  # adding brackets to each acronym since full form is present at only condition of giving the acronym at the end of full form within a bracket
        index = len(full_acronym)
        full_acronym.insert(index, words)  # inserting the acronyms with bracket to full_acronym list

    # searching the occurence of each acronym  for the first time in the document
    for word in full_acronym:
        with open("filepath", "r") as file:
            for line_number, line in enumerate(file, start=1):
                if word in line:
                    print(
                        f"The First occurance and Full form of the acronym '{word[1:-1]}' is found on line {line_number} of the document.")

    # seaching the words for which full form is not in the document
    for word in full_acronym:
        with open('filepath') as f:

            if word in f.read():
                pass
            else:
                print("The full form of acroname " + word[1:-1] + " is not found on the document.")
window = Tk()
button = Button(text="Open", command=openFile)
button.pack()
window.mainloop()

Solution

  • On this line with open('filepath', "r", errors='ignore') as file: you write the variable name as a string this why you got an error.

    Try this

    from tkinter import *
    from tkinter import filedialog
    import csv
    
    def openFile():
        filepath = filedialog.askopenfilename()
        acronyms = 0  # number of acronyms
        acronym_word = []  # creating a list to store acronyms found in the file
        with open(filepath, "r", errors='ignore') as file: @ edited
            text = str(file.read())
            for word in text.split(' '):  # for every word in line
                if word.isupper() and word.isalpha():  # if word is all uppercase letters
                    acronyms += 1
                    if len(word) == 1:  # ignoring the word found in the file of single character as they are not acronyms
                        pass
                    else:
                        index = len(acronym_word)
                        acronym_word.insert(index, word)  # storing all the acronyms founded in the file to a list
    
        uniqWords = sorted(set(acronym_word))  # remove duplicate words and sort the list of acronyms
        for word in uniqWords:
            print(word, ":",
                  acronym_word.count(word))  # printing the all the acronyms along with no of times they appear in the file
            with open('LOG.csv', 'w', newline='') as csvfile:  # creating the csv file
    
                fieldnames = ['Name_of_acronyms', 'Number_of_times_they_occured']
    
                thewriter = csv.DictWriter(csvfile, fieldnames=fieldnames)
    
                thewriter.writeheader()
    
                for word in uniqWords:
                    thewriter.writerow({'Name_of_acronyms': word,
                                        'Number_of_times_they_occured': acronym_word.count(word)})  # Extrating the CSV file
                    full_acronym = []  # creating a list of acronym for which we will search its full form occurence in the document
    
        for word in uniqWords:
            words = str(
                "(" + word + ")")  # adding brackets to each acronym since full form is present at only condition of giving the acronym at the end of full form within a bracket
            index = len(full_acronym)
            full_acronym.insert(index, words)  # inserting the acronyms with bracket to full_acronym list
    
        # searching the occurence of each acronym  for the first time in the document
        for word in full_acronym:
            with open("filepath", "r") as file:
                for line_number, line in enumerate(file, start=1):
                    if word in line:
                        print(
                            f"The First occurance and Full form of the acronym '{word[1:-1]}' is found on line {line_number} of the document.")
    
        # seaching the words for which full form is not in the document
        for word in full_acronym:
            with open('filepath') as f:
    
                if word in f.read():
                    pass
                else:
                    print("The full form of acroname " + word[1:-1] + " is not found on the document.")
    window = Tk()
    button = Button(text="Open", command=openFile)
    button.pack()
    window.mainloop()