Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8799913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:26:56+00:00 2026-06-14T00:26:56+00:00

In the following code i am trying to make a more command (unix) using

  • 0

In the following code i am trying to make a “more” command (unix) using python script by reading the file into a list and printing 10 lines at a time and then asking user do you want to print next 10 lines (Print More..).
Problem is that raw_input is asking again and again input if i give ‘y’ or ‘Y’ as input and do not continue with the while loop and if i give any other input the while loop brakes.
My code may not be best as am learning python.

import sys
import string
lines = open('/Users/abc/testfile.txt').readlines()
chunk = 10
start = 0

while 1:
    block = lines[start:chunk]
    for i in block:
        print i
    if raw_input('Print More..') not in ['y', 'Y']:
        break
    start = start + chunk

Output i am getting for this code is:-

--
10 lines from file

Print More..y
Print More..y
Print More..y
Print More..a
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:26:57+00:00Added an answer on June 14, 2026 at 12:26 am

    As @Tim Pietzcker pointed out, there’s no need of updating chunk here, just use start+10 instead of chunk.

    block = lines[start:start+10]
    

    and update start using start += 10.

    Another alternative solution using itertools.islice():

     with open("data1.txt") as f:
        slc=islice(f,5)            #replace 5 by 10 in your case
        for x in slc:
            print x.strip()
        while raw_input("wanna see more : ") in("y","Y"):     
            slc=islice(f,5)        #replace 5 by 10 in your case
            for x in slc:
                print x.strip()
    

    this outputs:

    1
    2
    3
    4
    5
    wanna see more : y
    6
    7
    8
    9
    10
    wanna see more : n
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to compile my gcc code using the following make command .
I'm trying to open a mpg-File using emguCV. I use the following code: if
i'm trying to make a php tcp/ip server with the following code: <?php //
I'm trying to use the following code to make an image fadeOut and, only
I am trying the following code in eclipse: public class A { List<Integer> intList
In the following code, I'm trying to make it so that when an input
I'm trying to make my code better by being more specific with the SQL
The following code is within an ajax call. I'm trying to make sure people
I'm trying to run some external executable code from within Python and then make
I got the following code and I'm trying to make it match on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.