[Tutor] How to list/process files with identical character strings
Alex Kleider
akleider at sonic.net
Wed Jun 25 00:55:58 CEST 2014
On 2014-06-24 14:01, mark murphy wrote:
> Hi Danny, Marc, Peter and Alex,
>
> Thanks for the responses! Very much appreciated.
>
> I will take these pointers and see what I can pull together.
>
> Thanks again to all of you for taking the time to help!
Assuming your files are ordered and therefore one's that need to be
paired will be next to each other,
and that you can get an ordered listing of their names,
here's a suggestion as to the sort of thing that might work:
f2process = None
for fname in listing:
if not f2process:
f2process = fname
elif to_be_paired(f2process, fname):
process(marry(f2process, fname))
already_processed = fname
f2process = None
else:
process(f2process)
already_processed = fname
f2process = fname
if fname != already_processed:
# I'm not sure if 'fname' survives the for/in statement.
# If it doesn't, another approach to not loosing the last file will
be required.
# I hope those more expert will comment.
process(fname)
def to_be_paired(f1, f2):
"""Returns a boolean: true if the files need to be amalgamated."""
pass # your code goes here.
def marry(f1, f2):
"""Returns a file object which is a combination of the two files
named by f1 and f2."""
pass # your code here.
def process(fname_or_object):
"""Accepts either a file name or a file object, Does what you want
done."""
pass # your code here.
Comments?
I was surprised that the use of dictionaries was suggested, especially
since we were told there were many many files.
More information about the Tutor
mailing list