[Tutor] How to compare the first line from multiple files

Shawn Milochik Shawn at Milochik.com
Thu Jan 31 20:33:41 CET 2008

-----Original Message-----
From: Sorghum Crow <sorcrow at gmail.com>
To: tutor at python.org
Subject: [Tutor] How to compare the first line from multiple files
Date: Thu, 31 Jan 2008 14:19:55 -0500

How to compare the first line from multiple files


 I'm new on the forum and relatively new to Python. I have been tasked
at work with checking some files for consistency.

 What I need to do is verify that the first lines of a group of text
files are identical.
 The files are all in the same directory.
 I know what the first line should be (or I can take the first line
from the first file as the reference.)

How would I go about doing this.

Thanks in advance.

Sorghum Crow
Tutor maillist  -  Tutor at python.org

Try this:

Find code which will loop through each file in a directory. Easy to do.

Open each file and read the first line into a variable.

Then do something fun with this info, such as adding it to a dictionary 
and incrementing the value for that dictionary value each time you find that line.

When you're done, you can check the length of the dictionary to see if there is more than
one value, and then view the contents of the dictionary to find out what the unique values are.


More information about the Tutor mailing list