On Jul 21, 12:02 pm, Gary <woody...@sky.com> wrote: -------------------------------------------------- > total = ' ' > os.chdir('/home/woodygar/Desktop/Docs') > for i in os.listdir('.'): -------------------------------------------------- "i" was a bad local var choice here! i and x are typically reserved to represent integer indexes in local loop scope whilst iterating over a container object. Likewise y and z are meant to represent the next levels of indexes. However when looping over a list of strings use something more appropriate like:
for filename in os.listdir('.'): do_something_with(filename) -------------------------------------------------- > if '.txt' in i: -------------------------------------------------- No, no, don't do that! Do if str.endswith('.txt') instead. Or use the os.path methods. -------------------------------------------------- > f = open(i, 'r') > total += f.read() > f.close() -------------------------------------------------- Two bad things happening here; (1.) Need to catch exceptions. try: f = open(i, 'r') total += f.read() except IOERROR: freak_out() else: f.close() (2.) NEVER concatenate a string with += in a loop! Instead load the strings into a list and then use ''.join(lst) method on the list. >>> filetext = """\ This is line one this is line two this is line four\ """ >>> lst = [] >>> for line in filetext.splitlines(): lst.append(line) >>> lst ['This is line one', 'this is line two', '', 'this is line four'] >>> '\n'.join(lst) 'This is line one\nthis is line two\n\nthis is line four' >>> print '\n'.join(lst) This is line one this is line two this is line four >>> -------------------------------------------------- > message = """\ > Subject: %s > %s > > """% (SUBJECT,total) -------------------------------------------------- Use the new format spec over the old string interpolation if available in your version of python. MSG = """ Subject: {0} {1} """ MSG.format(SUBJECT,total) -- http://mail.python.org/mailman/listinfo/python-list