[Tutor] How to compare the first line from multiple files
bgailer at alum.rpi.edu
Thu Jan 31 21:09:35 CET 2008
Michael Langford wrote:
> You should checkout os.listdir()
Which will give you a list of the filenames.
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
For that you need:
the open() built-in function
the file readline() method
the assignment, for, if and print statements
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
>> 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.)
919-636-4239 Chapel Hill, NC
More information about the Tutor