[Tutor] What is missing?

Rick Pasotto rick@niof.net
Sun, 3 Feb 2002 23:33:01 -0500


On Sun, Feb 03, 2002 at 11:15:20PM -0500, Ramkumar Kashyap wrote:
> 
> 
> import os
> import time
> 
> DIR = "D:\\dp_test"
> for filename in os.listdir(DIR):
>    full_path = os.path.join(DIR, filename)
>    myFile = open(full_path, 'r')
>    inFile = open('dp_hr.txt', 'w')
>    count = len(myFile.readlines())
>    print filename + ",", count     
>    inFile.write(filename + ', ' + string(count))
> 
> Hi!
> There are 325 files in the directory. The program works up until the 
> print statement which displays all the 325 files with the corresponding 
> lines contained in the respective files. The write statement only writes 
> the last file and line_numbers for the last file. Should I put it in 
> another loop?

Every time through the loop you re-open (thus truncating) inFile. Just
move that line above the loop and all will be well.

-- 
Within the limits of equity, everything is to be accomplished
through the free and perfectible initiative of man; nothing is to
be achieved by law or by force save universal justice.
	-- Frédéric Bastiat (1801-1850)
    Rick Pasotto    rickp@telocity.com    http://www.niof.net