473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Loop of a text file showing first 20 lines?

How can I code a loop of a text file showing it's first 20 lines and after pressing enter it shows the next 20 lines and so on?
Sep 30 '10 #1
5 2113
Oralloy
988 Expert 512MB
@huud,

Try putting something together and posting it, if you're having troubles with the code. We'll gladly help.

To get off square one with python, go try one of the tutorials that are all over the 'Net. It'll get you a lot farther than asking for code chunks.

Luck!
Sep 30 '10 #2
OK, I've got it to show the file's first line 20 times over and over again.
Expand|Select|Wrap|Line Numbers
  1. f = open("story.txt", "r")
  2. a = f.readlines()
  3. while True:
  4.     print 20*a
  5.  
  6.  
Sep 30 '10 #3
I've got it to show the first 20 lines of a text file over and over.
[code] story = open("11.txt", "r")
a = story.readline()
b = story.readline()
c = story.readline()
d = story.readline()
e = story.readline()
f = story.readline()
g = story.readline()
h = story.readline()
i = story.readline()
j = story.readline()
k = story.readline()
l = story.readline()
m = story.readline()
n = story.readline()
o = story.readline()
p = story.readline()
q = story.readline()
r = story.readline()
s = story.readline()
t = story.readline()
all = a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t
while True:
print all
[code]
There's gotta be a beter way to do this :D
Sep 30 '10 #4
Oralloy
988 Expert 512MB
Use an inner loop which does story.readline() once, and prints each individual line independently.

Then your outer loop can wait patiently for the next user input, then it releases the inner loop, etc....
Sep 30 '10 #5
bvdet
2,851 Expert Mod 2GB
Here's some pseudocode:

Open the file, assign to an identifier

Start a while loop, maybe while True:

Initialize an empty list

I am using file method next() which raises a StopIteration exception when reaching the end of a file, so start a try/except block

Start a for loop over a range of 20

Append the next line of the file to the list using file method next(), end of for loop

Print the list using string method join
Expand|Select|Wrap|Line Numbers
  1. '\n'.join(thelist)
Call raw_input() which pauses processing

Now for the except part of the try/except block (except StopIteration:)

If there is anything in the list, print it

Break from the loop
Oct 1 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
4
by: MJ | last post by:
Currently i am using the below coding to create a text file and write something on it. Dim wfile As New System.IO.StreamWriter("c:\test.txt" wfile.WriteLine("Hello" & vbcrlf & "Are" & vbcrlf &...
31
by: microsoft.news.com | last post by:
I need to read a text file but I need to start on line 2 of the file, How can I start reading the text file starting with the second line and not the first line in the file?
1
by: JenHu | last post by:
Hi all, I want to create a text file to write lines, but it gives me an error on Dim sw As New StreamWriter(CreatePath & Efilename) : System.IO.IOException: The process cannot access the file...
4
by: anne001 | last post by:
Hi For a class, students are going to run an experiment on line. Each time a subject runs, his/her data is appended to one giant text file. Their own data set will be just one line starting with...
14
by: mfrsousa | last post by:
hi there, i have a huge large text file (350.000 lines) that i want to import to a MS Acccess Database, of course i don't want to use Access, but do it with C#. i already have tried the...
13
by: JJ | last post by:
I have a need to input a large tab delimited text file, which I will parse to check it has the expected columns, before allowing the user to submit it to the database. The user may paste the file...
6
by: Prime Mover | last post by:
In C, if I have three variables obtained in a loop like: for(i=0;i<N;i++) { x = ....{expression here}; y = ....{expression here}; z = ....{expression here}; }
4
by: nxstar | last post by:
Hi I am confused about how to go about writing this program in perl. I have a text file with data in columns 8 and 9. I need to extract the data value from column 8 from the line being read and...
0
tuxalot
by: tuxalot | last post by:
I have a text file showing files in directories. The text file looks like this: "C:\folderA\file1.zip" "C:\folderA\folderB\folderC\file2.zip" "C:\folderA\folderE\folderX\folderU\file3.zip" ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.