Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

328
Views
Count the number of lines in a file using python and wc -l

I have this command on linux which I have problem converting into type on Windows:

row = run('cat '+'C:/Users/Kyle/Documents/final/VocabCorpus.txt'+" | wc -l").split()[0]

For the statement " wc - l" is for the line count to see how many lines exist. If I were to change it to the following using "type" command, what should it be?

I tried this and it doesnt work.

 row = run('type '+'C:/Users/Kyle/Documents/final/VocabCorpus.txt'+" | wc -l").split()[0]

The run command is below:

def run(command):
    output = subprocess.check_output(command, shell=True)
    return output

Please help me. Thank you.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You're trying to count the number of lines in a file? Why can't you do that in pure python?

Something like this?

with open('C:/Users/Kyle/Documents/final/VocabCorpus.txt') as f:
    row = len(f.readlines())
over 4 years ago · Santiago Trujillo Report

0

Actually wc counts \n symbols in your file (proof). If you have big files and want to save some memory, you'd better read it by chunks to have O(1) memory consumption:

CHUNK_SIZE = 4096

def wc_l(filepath):
    nlines = 0
    with open(filepath, 'rb') as f:
        while True:
            chunk = f.read(CHUNK_SIZE)
            if not chunk:
                break
            nlines += sum(1 for char in chunks if char == '\n')
    return nlines
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!