我知道這已經很多次了,但我沒有找到正確的答案。
第一:我正在做一個簡單的數據庫系統(爲我自己),將運行沒有哈希等(至少現在)。現在我陷入了困境。Pythons的AttributeError:'NoneType'對象沒有屬性'錯誤'
import sys
import os
filename = ""
database = ""
path = ""
table = ""
class Nollty:
returns = 0
errors = 0
def __init__(self, filename, database):
self.filename = filename
self.database = database
self.path = self.filename + "databases/" + self.database
openfile = open(self.path + "/db_required", "r")
if not openfile.errors:
self.returns = 1
if not os.path.exists(self.path + "/db_required"):
self.returns = 0
openfile.close();
def select(self, table):
errors = 0
self.table = table
openfile = open(self.path + "/" + self.table, "r")
if not openfile.errors:
errors = 1
if not os.path.exists(self.path + "/" + self.table):
errors = 0
openfile.close();
nollty = Nollty("", "test")
if nollty.returns == 1:
print "Successfully connected to the database!"
query = nollty.select("aaa_auto")
if query.errors == 0:
print "Successfully chosen the table!"
錯誤輸出是:
Traceback (most recent call last):
File "/home/spotrudloff/Python/Nollty/nollty.py", line 40, in <module>
if query.errors == 0:
AttributeError: 'NoneType' object has no attribute 'errors'
這個問題可能是,我是PHP程序員,我今天在幾個小時內學到的Python(所以我的想法仍然是「PHPy」)。
感謝您的所有回覆。
使選擇函數返回一些東西... – JBernardo 2012-01-14 01:43:34
請注意'openfile。錯誤「幾乎肯定不是你想象的那樣。它沒有設置爲錯誤代碼 - 它只是一個字符串,指定用於編碼的錯誤處理程序。在您的示例代碼中,它將始終具有值None。 (請參閱[文檔](http://docs.python.org/library/stdtypes.html#file.errors))。 – ekhumoro 2012-01-14 02:51:09
+ 1你的-1,因爲我發現這有幫助。 – ryanjdillon 2013-02-08 16:35:21