return statement in functions

hokieghal99 hokiegal99 at hotmail.com
Tue Dec 23 10:37:47 EST 2003


OK, I took the good advice I was given here and made my functions very
specific. I made them all return something sane... something one would
expect them to return. I even wrote a function to handle the report 
writing itself, but when it gets the output from the other functions to 
write the report file, it only writes this:

<function fs_object_count at 0x405d84c4>
<function clean_dir_names at 0x405d8534>
<function clean_file_names at 0x405d856c>
<function clean_dir_spaces at 0x405d85a4>
<function clean_file_spaces at 0x405d85dc>
<function doc_extension at 0x405d8614>
<function xls_extension at 0x405d864c>
<function pdf_extension at 0x405d8684>
<function ppt_extension at 0x405d86bc>
<function wpd_extension at 0x405d86f4>
<function jpg_extension at 0x405d872c>

I would like for it to write out what the functions returned. Obviously, 
I am missing something very fundamental here. Could someone hit me in 
the head with a hammer and help me understand this?

Here is an example of what I'm trying to do. Please be gentle with me. 
 From experience, I know that intelligence and modesty do not mix, but 
make an exception for me, just this once...

The ugly report function:

def report(a,b,c,d,e,f,g,h,i,j,k):
    outputFile = open('report.txt', 'w')
    print >> outputFile, a
    print >> outputFile, b
    print >> outputFile, c
    print >> outputFile, d
    print >> outputFile, e
    print >> outputFile, f
    print >> outputFile, g
    print >> outputFile, h
    print >> outputFile, i
    print >> outputFile, j
    print >> outputFile, k
    outputFile.close()

This function is an exception to my new "ALWAYS RETURN SOMETHING SANE" 
motto as I don't care what this returns.

Below is the first function where the output is taken as 'a' by the 
report function:

def fs_object_count(path):
    file_count = 0
    dir_count = 0
    for root, dirs, files in os.walk(path):
       file_count += len(files)
       dir_count += len(dirs)
    return dir_count, file_count

Here's one other example:

def doc_extension(path):
    for root, dirs, files in os.walk(path, topdown=False):
       for fname in files:
          doc_new = fname + '.doc'
          ms_id = string.find(file(os.path.join(root,fname), 
'rb').read(), 'Microsoft')
          doc_id = string.find(file(os.path.join(root,fname), 
'rb').read(), 'Word.Document.')
          ext = os.path.splitext(fname)
          if not ext[1] and doc_id >=1 and ms_id >=1:
             newpath = os.path.join(root,doc_new)
             oldpath = os.path.join(root,fname)
             os.renames(oldpath,newpath)
             return oldpath, newpath

Here's how I call the report function:

report(fs_object_count,clean_dir_names,etc., etc.)

Thanks!!!









More information about the Python-list mailing list