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

330
Views
Cuente el número de líneas en un archivo usando python y wc -l

Tengo este comando en Linux que tengo problemas para convertir en type en Windows:

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

Porque la instrucción "wc - l" es para el conteo de líneas para ver cuántas líneas existen. Si tuviera que cambiarlo a lo siguiente usando el comando "tipo", ¿cuál debería ser?

Intenté esto y no funciona.

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

El comando de ejecución está a continuación:

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

Por favor, ayúdame. Gracias.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

¿Está tratando de contar el número de líneas en un archivo? ¿Por qué no puedes hacer eso en Python puro?

¿Algo como esto?

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

0

En realidad, wc cuenta \n símbolos en su archivo ( prueba ). Si tiene archivos grandes y desea ahorrar algo de memoria, será mejor que lo lea por partes para tener un consumo de memoria O (1):

 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!