Python笔记(17)with语句

    技术2026-03-18  6

    #!/user/bin/env python # -*- coding:utf-8 -*- # author:berlin # # with的作用有两个: # # (1)为了避免打开文件后忘记关闭,可以通过管理上下文,当with代码块执行完毕时,内部会自动关闭并释放文件资源。 # with open('《yesterday once more》歌词','r') as sing: # for line in sing: # print(line) # (2)在python2,7后,with又支持同时对多个文件的上下文进行管理 with open('《yesterday once more》歌词','r') as sing1,\ open('《yesterday once more》歌词(bak)','r') as sing2: for line1 in sing1: print(line1) for line2 in sing2: print(line2)
    Processed: 0.008, SQL: 9