PDA

View Full Version : [SOLVED] help with python program



Despot Despondency
November 30th, 2009, 10:53 PM
Hi, I'm trying to write a simple python program that opens a file and prints the first N lines of it. Here's the code



#! /usr/bin/env python

import os

filename = raw_input('Enter file name: ')
N = raw_input('Enter numbers of lines to display: ')

fobj = open(filename)
allLines = fobj.readlines()

counter = 0

for eachLine in allLines:
counter += 1
if counter < N:
print eachLine,


The program opens the file I enter but it prints all the lines in the file instead of just the first N lines. Why is this happening? TAI

0cton
November 30th, 2009, 11:05 PM
First of N is not an integer (a number) so in python a string will always be bigger than an integer (I think, not sure)
so you need to convert it to an int, do N=int(N) (beware if the user types let's say "A" instead of a number :)
Also your approach is kinda inefficient, the loop will still get lines m but not print them (you are getting all the lines from the file but just printing some of them)
instead you should break after the first N lines are printed.
if counter>=N:
break
break will break out of the loop.
cheers.

DaithiF
November 30th, 2009, 11:09 PM
because N is a string, not a number. try converting it to an int first before doing the comparison against your counter.

Despot Despondency
November 30th, 2009, 11:17 PM
Excellent. I knew it was going to be something silly like that. Thanks for your help.

Can+~
December 1st, 2009, 12:33 AM
Notice that your code:


for eachLine in allLines:
counter += 1
if counter < N:
print eachLine,

Actually goes through the whole file, but just prints the first N strings. So you're wasting resources. With a simple break statement, you can end it:


filename = raw_input('Enter file name: ').strip()
maxlines = int(raw_input('Enter numbers of lines to display: '))

with open(filename) as infile:
for count,line in enumerate(infile):
print line

if count > maxlines:
break


(Using the "with" statement, and enumerate instead of a separate counter)

0cton
December 1st, 2009, 01:14 PM
Notice that your code:


for eachLine in allLines:
counter += 1
if counter < N:
print eachLine,

Actually goes through the whole file, but just prints the first N strings. So you're wasting resources. With a simple break statement, you can end it:


filename = raw_input('Enter file name: ').strip()
maxlines = int(raw_input('Enter numbers of lines to display: '))

with open(filename) as infile:
for count,line in enumerate(infile):
print line

if count > maxlines:
break


(Using the "with" statement, and enumerate instead of a separate counter)

I wonder if anyone actually read my reply :\

wmcbrine
December 1st, 2009, 03:47 PM
filename = raw_input('Enter file name: ').strip()
maxlines = int(raw_input('Enter numbers of lines to display: '))

with open(filename) as infile:
for count,line in enumerate(infile):
print line

if count > maxlines:
break

How about this?


filename = raw_input('Enter file name: ').strip()
maxlines = int(raw_input('Enter numbers of lines to display: '))

with open(filename) as infile:
for count in range(maxlines):
print infile.readline().strip('\n')