Python Forum
all i want to do is count the lines in each file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
all i want to do is count the lines in each file
#6
If I need to have quick look at number of lines in file then I use wc in terminal.

cat my_filename | wc -l
It is easy to use in Python with subprocess. Specific example on file named shakespeare.txt; check_output returns bytes so I converted it into integer:

>>> import subprocess
>>> source = subprocess.Popen(['cat', 'shakespeare.txt'], stdout=subprocess.PIPE)   
>>> lines = int(subprocess.check_output(['wc', '-l'], stdin=source.stdout))     
>>> lines
4155
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: all i want to do is count the lines in each file - by perfringo - May-19-2021, 07:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 1,773 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
  Row Count and coloumn count Yegor123 4 2,645 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Delete multiple lines from txt file Lky 6 3,916 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  failing to print not matched lines from second file tester_V 14 8,946 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Extracting Specific Lines from text file based on content. jokerfmj 8 5,486 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Importing a function from another file runs the old lines also dedesssse 6 3,734 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  [Solved] Trying to read specific lines from a file Laplace12 7 5,113 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  Split Characters As Lines in File quest_ 3 3,379 Dec-28-2020, 09:31 AM
Last Post: quest_
  How to use the count function from an Excel file using Python? jpy 2 5,976 Dec-21-2020, 12:30 AM
Last Post: jpy
  Find lines from one file in another tester_V 8 4,812 Nov-15-2020, 03:29 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020