#/usr/bin/env python #-*-coding:UTF-8-*- import sys """读取文件的常用方法""" def methodone(): fd = open("/home/userhome/hejoy/20170124.txt") try: for line in fd.readlines(): print line except IOError: print e finally: fd.close() def methodtwo(): try: for line in open("/home/userhome/hejoy/20170124.txt"): print line except IOError: print e def main():
methodone() methodtwo() if __name__=='__main__': main()
另外还有一种调用ConfigParser模块的方法,有兴趣可以研究下: