python - 为什么我会收到有关“NoneType”的错误消息?
问题描述
我有一个作为命令行脚本工作的程序,不能在 Gui 中工作。
原始代码工作正常。当我尝试实施它时,Tkinter
它会抱怨
TypeError:预期的 str、字节或 os.PathLike 对象,而不是 NoneType
我认为这意味着我正在尝试传递一个空值。我试过切换rb
到rt
. 没有帮助。
这是原始工作代码:
import csv
from operator import itemgetter
f = open('contacts_bounced.csv', 'rb')
reader = csv.reader(f)
reader = map(tuple,reader)
fn = open('contacts-2.csv', 'rb')
readern = csv.reader(fn)
readern = map(tuple, readern)
reader = map(itemgetter(0), reader)
unique = [item for item in readern if item[0] not in reader]
output = open("output.csv", 'wb')
wr = csv.writer(output)
for row in unique:
wr.writerow(row)
output.close()
这是不起作用的 Tkinter 代码:
1 # I've decided to implement this in one button instead of the original three. So, the only function that is being called here is strip()
2
3
4 win.title("CSV Processor")
5 win.geometry("600x400")
6
7 filename = ""
8 bncFile = ""
9 unique = ""
10
11
12 # Functions=======================================
13 def openFileDialog():
14 #root = tk.Tk()
15 filename = fd.askopenfilename(initialdir = ".", title = "Select your source file", filetypes = ( ("csv files", "*.csv"), ("all files", "*.*")))
16 srcLbl = tk.Label(text = filename)
17 srcLbl.grid(column=0, row=2)
18 return filename
19
20 def openBncFile():
21 bncfile = fd.askopenfilename(initialdir = ".", title = "Select your bounced addresses file", fil etypes = (("csv files", "*.csv"), ("all files","*.*")))
22 bncLbl = tk.Label(text = bncfile)
23 bncLbl.grid(column=0, row=3)
24
25 def strip():
26 filename = openFileDialog()
27 bncFile = openBncFile()
28
29 f = open(bncFile, 'rt')
30 reader = csv.reader(f)
31 reader = map(tuple, reader)
32
33 fn = open(filename, 'rt')
34 readern = csv.reader(fn)
35 readern = map(tuple, readern)
36
37 reader = map(itemgetter(0), reader)
38
39 unique = [item for item in readern if item[0] not in reader]
40
41 output = open("output.csv", 'wt')
42 wr = csv.writer(output)
43 for row in unique:
44 wr.writerow(row)
45 output.close()
47
48
49 # The buttons========================================
50 #pfBtn = tk.Button(text = "Choose your source file", command=openFileDialog, fg="green")
51 #pfBtn.grid(column=1, row=2)
52 #
53 #bBtn = tk.Button(text = "Choose your bounces file", command = openBncFile,fg="red")
54 #bBtn.grid(column=1, row=3)
55 #
56 procBtn = tk.Button(text = "Process", command = strip, fg = "blue")
57 procBtn.grid(column=1, row=4)
58
59
60
61 win.mainloop()
工作代码在当前目录中创建和“output.csv”文件。
解决方案
您忘记返回,openBncFile()
所以默认情况下 python 返回None
这是传递给open
函数的内容。
推荐阅读
- javascript - 从元素方法中获取元素的选择器
- wordpress - 分类档案中的多个自定义帖子类型导致白屏
- kubernetes - OrientDB 分布式模式下无法创建数据库
- javascript - 带有 Javascript 的滑块,从最后一个元素开始,我如何让它以正常顺序开始?
- python - 具有 2 个和更多变量的 python pandas lambda
- google-app-maker - 无法在 appmaker 中导入 .zip 文件
- python - 访问 GridSearch 的中间(和最终)模型转换
- azure-devops - 当所有测试都通过时,Azure DevOps Build Pipeline 中的 Chutzpah Jasmine 测试返回失败
- mysql - 错误 1054 - “on 子句”中的未知列“Store.Orders_ID”
- python - 将数据从excel导入python时出现名称错误