2006年01月06日 星期五 02:23
import sys if len(sys.argv)!=3: print u'Usage: mygrep filename findword' sys.exit(1) tmp,filename,findword=sys.argv j=0 try: for x in open(filename,'r'): if x.find(findword)!=-1: j+=1 print j,x except Exception,e: print e 象上面代码中 for x in open(filename,'r') 这样的写法,在程序运行结束后会自动关闭文件吗?或者是不是应该避免这样写程序? -- 开飞机的舒克 http://www.lvye.org/shuke msn:weizhong at netease.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060106/6a8acb47/attachment-0001.htm
2006年01月06日 星期五 02:58
会 在 06-1-6,魏忠<weizhong2004 at gmail.com> 写道: > import sys > if len(sys.argv)!=3: > print u'Usage: mygrep filename findword' > sys.exit(1) > tmp,filename,findword=sys.argv > j=0 > try: > for x in open(filename,'r'): > if x.find(findword)!=-1: > j+=1 > print j,x > except Exception,e: > print e > > 象上面代码中 for x in open(filename,'r') > 这样的写法,在程序运行结束后会自动关闭文件吗?或者是不是应该避免这样写程序? > > -- > > 开飞机的舒克 > http://www.lvye.org/shuke > msn:weizhong at netease.com > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > >
2006年01月13日 星期五 22:34
try: pass # file or network work finally: clean-up # always do clean, but don't nest an exception here! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060113/1038c528/attachment.htm
Zeuux © 2025
京ICP备05028076号