zoukankan      html  css  js  c++  java
  • LPTHW 笨办法学python 20章

    本章节讲述了,函数和文件的综合操作。

    分别 执行了。1、读出文件所有内容,2、把文件重置至文件开头。3、打印一行。

    我在本节作了一个小小的改良,设置了一个全局变量,记录当前应该输入哪一行,如果执行过一次,我就把这个行数加一。

    代码部分如下:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from sys import argv
    script, input_file = argv
    def print_all(f):
        print f.read()
    def rewind(f):
        global line_no
        f.seek(0, 0)
        line_no = 1
    def print_a_line(line_count,f):
        global line_no
        line_count = line_no
        print line_count,f.readline()
        line_no += 1
    
    current_file = open(input_file)
    print "First let's print the whole file:
    "
    print_all(current_file)
    
    print "Now let's rewind, kind of like a tape."
    rewind(current_file)
    
    print "Let's print three lines:"
    for i in xrange(3):
        print_a_line(line_no,current_file)
  • 相关阅读:
    [NOI2016] 网格
    [十二省联考2019]春节十二响
    wordcloud的方法参数归纳汇总
    选择困难症
    连通能力
    [Tjoi2017]城市
    bzoj3732 Network
    bzoj3252 攻略
    Noip2018旅行
    [HEOI2015]兔子与樱花
  • 原文地址:https://www.cnblogs.com/sageskr/p/4077589.html
Copyright © 2011-2022 走看看