Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

329
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda