From 796bd6853b2986d1449e0db78177b91e2ab83b21 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 10 Aug 2016 19:19:23 -0400 Subject: [PATCH] count number of lines pythonically, remove dep on wc and subprocess --- db_converter.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/db_converter.py b/db_converter.py index 7f1bc9a..d6a0700 100644 --- a/db_converter.py +++ b/db_converter.py @@ -12,17 +12,11 @@ import sys import os import time -import subprocess def parse(input_filename, output_filename): "Feed it a file, and it'll output a fixed one" - # State storage - if input_filename == "-": - num_lines = -1 - else: - num_lines = int(subprocess.check_output(["wc", "-l", input_filename]).strip().split()[0]) tables = {} current_table = None creation_lines = [] @@ -43,10 +37,14 @@ def parse(input_filename, output_filename): output = open(output_filename, "w") logging = sys.stdout + # Open the input file and state storage if input_filename == "-": input_fh = sys.stdin + num_lines = -1 else: input_fh = open(input_filename) + num_lines = sum(1 for l in input_fh) + input_fh.seek(0) output.write("-- Converted by db_converter\n")