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

bob gailer bgailer at alum.rpi.edu
Thu Jan 31 21:09:35 CET 2008


Michael Langford wrote:
> You should checkout os.listdir()
>
> http://docs.python.org/lib/os-file-dir.html
>   
Which will give you a list of the filenames.

Then (pseudo-code):
  open the first file, read its first line and save that in a variable
  loop thru the rest of the filenames
    open each, read the first line and compare it to the variable
     if comparison fails
       report that

For that you need:
   the open() built-in function
   the file readline() method
   the assignment, for, if and print statements
   comparison

Rather than one of us writing it for you, give it a try.
Show us what you wrote, what responses you get when you run it, what 
questions you have.
We'll take it from there.

> On Jan 31, 2008 2:19 PM, Sorghum Crow <sorcrow at gmail.com> wrote:
>   
>> How to compare the first line from multiple files
>>
>> Greetings,
>>
>>  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.)


-- 
Bob Gailer
919-636-4239 Chapel Hill, NC



More information about the Tutor mailing list