草庐IT

try-catch-repeat

全部标签

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中删除数组的所有其他元素? (np.repeat() 的逆?)

如果我有一个数组x,并执行np.repeat(x,2),我实际上是在复制数组。>>>x=np.array([1,2,3,4])>>>np.repeat(x,2)array([1,1,2,2,3,3,4,4])我怎样才能反其道而行之,最终得到原始数组?它也应该与随机数组y一起使用:>>>y=np.array([1,7,9,2,2,8,5,3,4])如何删除所有其他元素以得到以下结果?array([7,2,8,3]) 最佳答案 y[1::2]应该可以完成这项工作。这里第二个元素是通过以1为索引选择的,然后以2的间隔取。

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#中所做的也不符合错误处理原则。如果您对此有任何想法,我将不胜感激。 最佳答

Python:try-except 作为表达式?

我发现自己一遍又一遍地有这种模式:variable=""try:variable=...dosomefileloadingstuff...except:variable=""有什么办法可以把它浓缩成一个表达式?与if-else语句一样,您可以转:variable=""ifsomething:variable=somethingelseelse:variable=""进入variable=somethingelseifsomethingelse""try-catch有什么等价的东西吗? 最佳答案 因为agf已经提供了我推荐的方法,所以