嗨,我正在慢慢地学习编写python代码的正确方法。假设我有一个文本文件,我想检查它是否为空,我希望程序立即终止,如果确实是空的,控制台窗口会显示一条错误消息。到目前为止,我所做的事情写在下面。请教我如何处理这个案子:
import os
def main():
f1name = 'f1.txt'
f1Cont = open(f1name,'r')
if not f1Cont:
print '%s is an empty file' %f1name
os.system ('pause')
#other code
if __name__ == '__main__':
main()
可能是this的副本。在
从最初的答案:
Python的方法是:
不需要} 。在
open()
该文件,只需使用^{相关问题 更多 >
编程相关推荐