python looping through input file

后端 未结 4 1364
抹茶落季
抹茶落季 2020-12-16 15:07

My question is related to file-input in Python, using open(). I have a text file mytext.txt with 3 lines. I am trying to do two things with this fi

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 15:21

    I thin the module fileinput is you want.

    Here is the link

    if __name__ == "__main__":
    for line in fileinput.input():
        if fileinput.isfirstline():
            print("current file: %s" % fileinput.filename())
    
        print("line number: %d, current file number: %d" % 
              (fileinput.lineno(), fileinput.filelineno()))
    

提交回复
热议问题