Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:异常必须是旧式类或从BaseException派生的,而不是str_Python_Exception - Fatal编程技术网

Python TypeError:异常必须是旧式类或从BaseException派生的,而不是str

Python TypeError:异常必须是旧式类或从BaseException派生的,而不是str,python,exception,Python,Exception,我是在OS X 10.8上运行2.7.6的Python的初学者,添加了numpy和pyobjc。以下是我正在尝试运行的脚本: from __future__ import with_statement from Foundation import NSMutableDictionary, NSUserDefaults, NSCFArray, objc import numpy as np from copy import copy import os import re domainName

我是在OS X 10.8上运行2.7.6的Python的初学者,添加了numpy和pyobjc。以下是我正在尝试运行的脚本:

from __future__ import with_statement
from Foundation import NSMutableDictionary, NSUserDefaults, NSCFArray, objc
import numpy as np
from copy import copy
import os
import re

domainName = "org.mworks-project.MWClient"

outFile = os.path.expanduser(os.path.join('~/Desktop','org.Behavior.MWClientSavedVars.plist'))
keyNames = [ 
  'MATLAB client window - selected variables',
  'MATLAB client window - MATLAB .m file',
  'recentPythonScripts' ]

homedir = os.getenv('HOME')

################

def subStr(inStr):
    return re.sub('^%s'%homedir, '$HOME', inStr)

def replaceUserdirWithStr(inObj):
    if type(inObj) == str or type(inObj) == objc.pyobjc_unicode:
        return subStr(inObj)
    elif isinstance(inObj, NSCFArray):
        for i in range(len(inObj)):
            # do this recursively
            inObj[i] = replaceUserdirWithStr(inObj[i])
        return inObj
    else:
        print type(inObj)
        #import pdb; pdb.set_trace()
        raise 'Error: Type unknown'
    return 

################

# get client defaults
standardUserDefaults = NSUserDefaults.standardUserDefaults()
clientDefs = standardUserDefaults.persistentDomainForName_(domainName)

# copy the fields we need
writeDict = NSMutableDictionary.dictionary()
for k in clientDefs:
  if k in keyNames:
    tVal = clientDefs[k]
    tVal = replaceUserdirWithStr(tVal)
    writeDict[k] = tVal
success = writeDict.writeToFile_atomically_(outFile, 1)
############################################################
尝试运行此脚本但遇到此错误时:

<objective-c class __NSCFArray at 0x7fff7b9ea3c0>
Traceback (most recent call last):
  File "nameOfTheFileHere.py", line 60, in <module>
    tVal = replaceUserdirWithStr(tVal)
  File "nameOfTheFileHere.py", line 45, in replaceUserdirWithStr
    raise 'Error: Type unknown'
TypeError: exceptions must be old-style classes or derived from BaseException, not str

回溯(最近一次呼叫最后一次):
文件“nameoffilehere.py”,第60行,在
tVal=replaceUserDirWithTR(tVal)
文件“nameoffilehere.py”,第45行,在replaceUserdirWithStr中
引发“错误:类型未知”
TypeError:异常必须是旧式类或从BaseException派生的,而不是str
我被难住了。有人知道怎么解决这个问题吗?

这条线

raise 'Error: Type unknown'
无效,因为您正试图引发
str
而不是
异常

您想做一些更像:

raise TypeError('Type unknown')
我假设TypeError只是因为您的消息是“未知类型”

有关更多信息,请阅读有关引发异常的文档: 这条线

raise 'Error: Type unknown'
无效,因为您正试图引发
str
而不是
异常

您想做一些更像:

raise TypeError('Type unknown')
我假设TypeError只是因为您的消息是“未知类型”

有关更多信息,请阅读有关引发异常的文档:

不要使用命令运行python脚本。/script.py,请尝试以下操作

Python Script.py


请注意,

不要使用命令./script.py运行python脚本,而是尝试如下操作

Python Script.py

关于,

可能的副本