【发布时间】:2019-04-05 07:37:21
【问题描述】:
我正在开发一个允许用户使用一个按钮选择文件的程序:
def select_file(self):
filename = tkinter.filedialog.askopenfilename(initialdir=".")
infile = open(filename, "r")
另一个按钮,标记为计数出现,应该将 txt 文件读入字符串以搜索用户输入的内容:
def count_occurrences(self):
user_file = open(infile, "r")
txt_file = user_file.read()
# (omitted the code for counting occurrences for the sake of relevance)
我不确定问题出在哪个功能上,或者两者兼而有之。
单击“选择文件”按钮后,标签中会显示一个目录名称,但是当我在输入搜索文本后单击“计数出现次数”按钮时,出现错误:
"user_file = open(filename, "r") FileNotFoundError: [Errno 2] 没有这样的文件或目录:''
任何帮助将不胜感激,谢谢!
【问题讨论】:
标签: python python-3.x tkinter