Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»General Articles»How to Count Number of Lines in File with Python

    How to Count Number of Lines in File with Python

    RahulBy RahulJune 16, 20191 Min ReadUpdated:September 16, 2019

    This is a small Python script to count the number of lines in a text file. To run this script you must have Python installed on your system. Now save below script in a Python script (eg: count.py) and update test.txt with your filename in the script to which you need to count lines.

    vim count.py
    

    Python
    1
    2
    3
    4
    5
    6
    fname = "test.txt"
    count = 0
    with open(fname, 'r') as f:
        for line in f:
            count += 1
    print("Total number of lines is:", count)

    Then execute the script on the command line and see the result. This will show you the number of lines available in a file.

    python3 count.py
    
    Total number of lines is: 513
    
    Python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Rename a Column in SQL Server
    Next Article How to Install AnyDesk on Fedora (TeamViewer Alternative )

    Related Posts

    How to Find Django Install Location in Linux

    Updated:April 27, 20221 Min Read

    (Resolved) – ReactJS 404 Error on Page Reload

    2 Mins Read

    Adding a New SSH Key in GitHub

    Updated:April 1, 20223 Mins Read

    13 Best Linux Terminal Emulators and Bash Editors

    8 Mins Read

    How To Install Oracle VirtualBox on Debian 11

    2 Mins Read

    How To Install Python 3.10 on Debian 11/10

    2 Mins Read

    1 Comment

    1. NUR IZYANI on December 4, 2020 2:11 pm

      hello, I’m from malaysia.
      i’m doing my python exercise using Pycharm.
      can you help me solve this coding?

      def main(name, number):
      try:
      name[number] = name[number] + 1
      except KeyError as e:
      name[number] = 1
      return

      if __name__ == ‘__main__’:
      a_dictionary = {}
      pl = open(‘phonelist.txt’, ‘r’)
      for data in pl:
      main(name, number)
      for name, number in a_dictionary.items():
      print(name + ” – ” + number + ” : ” + str(number) + ” times”)

      *phonelist.txt*
      abu 0124512123
      azman 0137894561
      azman 0137894561
      ali 0194022433
      abu 0124512123
      azman 0137894561
      James 0194789632

      ***this is simple exercise to count the duplicate line in .txt files

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    • (Resolved) Please install all available updates for your release before upgrading
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.