Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

327
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda