1

I actually have two questions.

lines = file.readlines()

1.) Is variable lines a list?

2.) How to see how many elements are there in a list?

Thank you very much :D

Gloripaxis
  • 1,026
  • 1
  • 9
  • 9

1 Answers1

1
with open('p7.erl', 'r') as f:
    lines = f.readlines()
    print type(lines)
    print len(lines)

outputs:

<type 'list'>
27

so yes, lines is a list and you can find out how many elements are in the by len function.

If you are only interested in number of lines in a file see this

Community
  • 1
  • 1
cashmere
  • 2,811
  • 1
  • 23
  • 32
  • Thank you very much! I would vote up but I'm level 13, and I must be 15. May I ask why do you use with open('p7.erl', 'r') as f: instead of f = open('p7.erl', 'r') ? I think it's all about personal preferences, isn't it? – Gloripaxis Apr 04 '13 at 19:08
  • 1
    You don't have to worry about closing the file (eg: an exception occurs before you close it) after you are done with it if you use `with`. It will be taken care of when you go outside with block. You can read upon with [here](http://effbot.org/zone/python-with-statement.htm) to understand it better. – cashmere Apr 04 '13 at 19:12