为什么会收到有关“ NoneType”的错误消息?

时间:2019-08-12 13:13:01

标签: python user-interface modal-dialog

我有一个程序可以作为命令行脚本使用,而不能在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”文件。

1 个答案:

答案 0 :(得分:1)

您忘记了返回openBncFile(),因此默认情况下python返回None,这就是传递给open函数的内容。