在shell中工作但不是作为程序?

时间:2015-06-13 14:49:47

标签: python python-3.x

我在shell中尝试了以下内容

dyld: Library not loaded: @rpath/Pods.framework/Pods
  Referenced from: /private/var/mobile/Containers/Bundle/Application/...
  Reason: image not found

它返回文件中的文本,这是我想要它做的。但是,当我写它并将其保存为程序时

infile = open("studentinfo.txt", "r")
infile.read()

它刚刚返回空行。

1 个答案:

答案 0 :(得分:6)

您的函数永远不会返回值,因此会再次丢弃它们。

添加return声明:

def main():
    infile = open("studentinfo.txt", "r")
    return infile.read()

此外,在交互式解释器中,除非结果为None,否则所有表达式结果都将自动回显。在常规脚本中,您必须明确打印结果:

print(main())
相关问题