小编Sre*_*nat的帖子

python try:except:finally

# Open new file to write
file = None
try:
    file = open(filePath, 'w')
except IOError:
    msg = ("Unable to create file on disk.")
    file.close()
    return
finally:
    file.write("Hello World!")
    file.close()
Run Code Online (Sandbox Code Playgroud)

上面的代码是从函数中删除的.用户的一个系统正在报告错误:

file.write("Hello World!")
Run Code Online (Sandbox Code Playgroud)

错误:

AttributeError: 'NoneType' object has no attribute 'write'
Run Code Online (Sandbox Code Playgroud)

问题是,如果python未能打开给定文件,'except'块执行并且必须返回,但是控制将转移到抛出给定错误的行.'file'变量的值为'None'.

有什么指针吗?

python file-io exception try-catch

44
推荐指数
3
解决办法
8万
查看次数

如何在C中关闭stdout的缓冲

我想关闭stdout的缓冲以获取以下代码的确切结果

while(1) {
printf(".");
sleep(1);
}
Run Code Online (Sandbox Code Playgroud)

代码printf一堆'.' 只有当缓冲区被填满时

c

32
推荐指数
3
解决办法
3万
查看次数

调用高阶函数时出错

我写了一个更高阶的Haskell函数如下,

higherOrderFun f p xs = (map f) (filter p xs)
Run Code Online (Sandbox Code Playgroud)

它适用于以下情况

higherOrderFun (\x -> 2 * x) odd [1..4]

but throws an error for

higherOrderFun sin odd [1..4]
Run Code Online (Sandbox Code Playgroud)

这是堆栈跟踪:

No instance for (Show b0) arising from a use of ‘print’
The type variable ‘b0’ is ambiguous
Note: there are several potential instances:
  instance Show Double -- Defined in ‘GHC.Float’
  instance Show Float -- Defined in ‘GHC.Float’
  instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
    -- Defined in ‘GHC.Real’ …
Run Code Online (Sandbox Code Playgroud)

haskell ghci

1
推荐指数
1
解决办法
180
查看次数

标签 统计

c ×1

exception ×1

file-io ×1

ghci ×1

haskell ×1

python ×1

try-catch ×1