[Tutor] IPcount = 0 when used with subprocess.call (use something to convert to integer?)

Prasad, Ramit ramit.prasad at jpmorgan.com
Tue May 8 18:17:25 CEST 2012


> def count_lines(filename):
>     with open(f, 'r') as in_stream:
>         return len(in_stream.readlines())
> 
> The count_lines function takes a filename and returns the number of
> lines in that file.  This way you avoid the problems with shell
> metacharacters when using shell=True, mentioned in the subprocess
> documentation:
> http://docs.python.org/py3k/library/subprocess.html#frequently-used-
> arguments
>

Or for large files where you do not want to read the entire file
to memory at the same time (assuming Python 2.x; not sure if 3.x 
stops the variable leakage I use here)

def count_lines(filename):
    with open(f, 'r') as in_stream:
         idx = 0
         for idx, line in enumerate(in_stream, 1):
             pass
         return idx
Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list