Calculate the number of rows in a file with a Python script

Source: Internet
Author: User
Python can count the number of lines in a file, do you believe it? Whether you believe it or not, I believe it anyway. Let's take a look at how Python counts the number of rows in a file, the code is simple, I also make comments, very simple implementation ...

1

2

3

4

5

6

7

8

9

#_ *_coding:utf_8# introduce SYS and System OS module import SYS import OS "Calculate the number of rows of a file" def countfilelines (filename):     count = 0    Try :         handle = open (filename, ' R ')         #循环文件句柄for eachline in handle:            #累加            count + = 1    #异常捕获    except IOError, E:         print ' File open error ', e    return count     print countfilelines ('/home/pythontab/tmp/a.php ')
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.