I'm having trouble getting this program to keep going through the lines in the first loop. It just prints out the output for the first (searchline) and stops. Can anyone help me to figure out why it won't keep reading the lines in the birthday file?

import string

def find():
	
	inPiDigits = open("pidigits.txt", "r")
	inBirthday = open("birthday.txt", "r")
	blength = len(open("birthday.txt").readlines())
	length = len(open("pidigits.txt").readlines())
	for line in range(blength):
		searchline = inBirthday.readline()
	
			
				
		for lines in range(length):
			linePiDigits = inPiDigits.readline()
			search = linePiDigits.find(searchline[:6])
			if search >=0:
				print lines+1, search
		
find()

Generally you can simplify your code to ...

def find():
    for bd_line in open("birthday.txt"):
        for pi_line in open("pidigits.txt"):
            ix = pi_line.find(bd_line[:6])
            if ix >= 0:
                print "birthday %s is at index %s in pi line\n%s" % \
                    (bd_line[:6], ix, pi_line)
        
find()

Wow that simplified it a lot. How does that work without the readline?
Also do you know how I could print out the 3rd line? It is supposed to be at index 48, but I don't know how I could make the program read between the two lines.

also is there a way for me to print out the line number i found the date on?

Wow that simplified it a lot. How does that work without the readline?
Also do you know how I could print out the 3rd line? It is supposed to be at index 48, but I don't know how I could make the program read between the two lines.

Python code like
for bd_line in open("birthday.txt"):
actually uses readlines as a generator ( xreadlines() ).

If the birthday is between two lines, you have to temporarily store one line and then combine it with the next line that comes up. Then do the search.

also is there a way for me to print out the line number i found the date on?

Python has a function enumerate() that will count the lines for you, simply change your code to ...

def find():
    for bd_line in open("birthday.txt"):
        for num, pi_line in enumerate( open("pidigits.txt") ):
            ix = pi_line.find(bd_line[:6])
            if ix >= 0:
                print "birthday %s is at index %s in pi line #%d\n%s" % \
                    (bd_line[:6], ix, num, pi_line)
        
find()

Do the open files get closed? If so, when?

Python will close the file when the generator/iterator is done.

Would you know where in the docs it says that?

You are right, it may not close immediately , but how would you know or close it, since there is no file handle. All I remember reading is that the memory manager will close the file when it has a chance.

That's what I was wondering :D

perhaps

with open("birthday.txt") as f:
    for bd_line = f:
       etc.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.