首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么没有找到文件目录?

为什么没有找到文件目录?
EN

Stack Overflow用户
提问于 2022-09-12 04:50:07
回答 1查看 31关注 0票数 1

它给了我FileNotFoundError: Errno 2,没有这样的文件或目录:'filepath‘错误。而我已经定义了函数"filepath“,并在适当的位置调用它。当我运行代码时,按钮会出现,然后,当我为它分配脚本应该操作的文件时,它会显示上面的错误。

代码语言:javascript
复制
#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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-12 04:54:32

在这一行with open('filepath', "r", errors='ignore') as file:中,将变量名作为字符串写入,这就是为什么会出现错误。

尝尝这个

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73684611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档