草庐IT

try-catch-rethrow

全部标签

python - `with` s 中有几个 `try` s

我有几个可能的文件可以保存我的数据;它们可以用不同的方式压缩,所以要打开它们我需要使用file()、gzip.GzipFile()和其他也返回一个文件对象(支持带接口(interface))。我想尝试每一个,直到一个成功打开,所以我可以做类似的事情try:withgzip.GzipFile(fn+'.gz')asf:result=process(f)except(IOError,MaybeSomeGzipExceptions):try:withxCompressLib.xCompressFile(fn+'.x')asf:result=process(f)except(IOError,M

python - 如何防止 try catch python中的所有可能行?

我连续有很多行可能会引发异常,但无论如何,它仍应继续下一行。如何在不单独trycatch每个可能引发异常的语句的情况下执行此操作?try:this_may_cause_an_exception()but_I_still_wanna_run_this()and_this()and_also_this()exceptException,e:logging.exception('Anerrormaybeoccuredinoneoffirstoccuringfunctionscausingtheothersnottobeexecuted.Locals:{locals}'.format(loca

python - 导入错误 : No module named '_curses' when trying to import blessings

我正在尝试运行这个:fromblessingsimportTerminalt=Terminal()print(t.bold('Hithere!'))print(t.bold_red_on_bright_green('Ithurtsmyeyes!'))witht.location(0,t.height-1):print('Thisisatthebottom.')这里的第一个例子是:https://pypi.python.org/pypi/blessings.但是,我收到此错误:Traceback(mostrecentcalllast):File"",line1,inFile"C:\Use

python - 错误 : "You are trying to add a non-nullable field"

我定义了下面的模型并得到错误:您正在尝试在没有默认值的情况下向videodata添加不可为空的字段“用户”;我们不能这样做models.pyclassUser(Model):userID=models.IntegerField()userName=models.CharField(max_length=40)email=models.EmailField()classMeta:ordering=['userName']verbose_name='UserMetaData'verbose_name_plural='UsersMetaData'def__unicode__(self):re

python - Python *with* 语句是否完全等同于 try - (except) - finally block ?

我知道这已被广泛讨论,但我仍然找不到答案来确认这一点:with语句是否与在try-(except)-finallyblock中调用相同的代码相同,上下文管理器的__exit__函数中定义的任何内容都放在finallyblock中?例如--这2个代码片段是否在做完全相同的事情?importsysfromcontextlibimportcontextmanager@contextmanagerdefopen_input(fpath):fd=open(fpath)iffpathelsesys.stdintry:yieldfdfinally:fd.close()withopen_input("

python - 检查文件是否可以用 Python : try or if/else? 读取

我有以下代码:importglob,osforfileinglob.glob("\\*.txt"):ifos.access(file,os.R_OK):#Dosomethingelse:ifnotos.access(file,os.R_OK):print(file,"isnotreadable")else:print("Somethingwentwrongwithfile/dir",file)break但我不完全确定这是否是正确的做法。使用try和catch错误会更好吗?如果是这样,我该如何尝试以提高可读性?请注意我的else语句中的break。一旦无法读取文件,我就想中止循环。

python - 如何快速禁用 python 中的 try 语句进行测试?

假设我有以下代码:try:print'foo'#Alotmorecode...print'bar'except:pass出于测试目的,我如何临时禁用try-statement?你不能只注释掉try和except行,因为缩进仍然是关闭的。没有比这更简单的方法了吗?#try:print'foo'#Alotmorecode...print'bar'#except:#pass 最佳答案 您可以将异常重新引发为您的exceptblock的第一行,它的行为就像没有try/except一样。try:print'foo'#Alotmorecode.

python - IOError : [Errno 2] No such file or directory trying to open a file

这个问题在这里已经有了答案:open()givesFileNotFoundError/IOError:Errno2Nosuchfileordirectory(8个回答)Whydoesn'tcallingastringmethod(suchas.replace)modify(mutate)thestring?Whydoesn'titchangeunlessIassigntheresult?(3个回答)关闭2个月前。我对Python很陌生,所以请原谅以下基本代码和问题,但我一直在试图找出导致我遇到错误的原因(我什至在S.O.上查看过类似的线程)但不能解决我的问题。这是我想要做的:循环浏览包

python - IOError : [Errno 13] Permission denied when trying to open hidden file in "w" mode

我想替换一个隐藏文件的内容,所以我尝试在w模式下打开它,这样它就会被删除/截断:>>>importos>>>ini_path='.picasa.ini'>>>os.path.exists(ini_path)True>>>os.access(ini_path,os.W_OK)True>>>ini_handle=open(ini_path,'w')但这导致了回溯:IOError:[Errno13]Permissiondenied:'.picasa.ini'但是,我能够通过r+模式达到预期的效果:>>>ini_handle=open(ini_path,'r+')>>>ini_handle.t

python - 带有空 except 代码的 Try-except 子句

这个问题在这里已经有了答案:Howtoproperlyignoreexceptions(12个回答)关闭8年前。有时您不想在except部分中放置任何代码,因为您只想确保代码运行时没有任何错误,但对捕获它们不感兴趣。我可以在C#中这样做:try{do_something()}catch(...){}我怎么能在Python中做到这一点?,因为缩进不允许这样做:try:do_something()except:i_must_enter_somecode_here()顺便说一句,也许我在C#中所做的也不符合错误处理原则。如果您对此有任何想法,我将不胜感激。 最佳答