Q:

script to find 777 files and folders how to improve and make it compatible in python 2.4 (RHEL5)

i have the following code, my first ever python script. it is supposed to find the files and folders with 777 permissions while excluding some folders like /proc, etc 
is there a way I can improve the script? 
#!/usr/bin/env python

import os, sys, socket,csv

from os.path import join

mode = int('777', 8) 
results = {} 
host = socket.gethostname() 
results[host] = {}

exclude = ['proc', 'run']

def get_username(uid):
    import pwd
    try:
        return pwd.getpwuid(uid).pw_name
    except KeyError:
        return uid

def find_files():
    for (dirpath, dirnames, filenames) in os.walk('/'):
        dirnames[:] = [d for d in dirnames if d not in exclude]
        listoffiles = [join(dirpath, file) for file in filenames]
        listoffiles += [join(dirpath,dir) for dir in dirnames]
        for path in listoffiles:
            try:
                statinfo = os.stat(path)
            except OSError:
               #print(path)
               pass
            if (statinfo.st_mode & 0o777) == mode:
                results[host][path] = {}
                results[host][path]['owner'] = get_username(statinfo.st_uid)
                results[host][path]['perm']  = oct(statinfo.st_mode & 0o777)
    return results

find_files()

resultstxt = csv.writer(open('results_%s.csv' % host, 'w')) for hostname,data in results.items():
    for path, attributes in data.items():
      resultstxt.writerow([hostname, path, attributes['perm'], str(attributes['owner'])])

I would also need to modify it because we have  a couple of legacy rhel5 servers and the code does not work with a strange syntax error:
 File "./find777.py", line 34
    if (statinfo.st_mode & 0o777) == mode:
                               ^

A:

Use 0777 instead of 0o777.
The 0o prefix for octal numbers wasn't available in Python 2.4. The traditional 0 prefix works until Python 3.

