I have a block of code that i am wanting to turn into a function and i am very ocnfused on how to do it properly. If anyone could give me a lead on how to do this it would be a great help. The for statement is where i want to call the function so taht it loops through the folder its searching.

for filename in glob.glob(fileFull):
    statinfo = os.stat(filename)
for Filename in glob.glob(fileAbriv):
    Statinfo = os.stat(Filename)

SUBJECT = 'File size of '+fileFull
Subject = 'File size of '+fileAbriv
BODY = 'The file size of '+fileFull+' was greater than '+str(statinfo.st_size)+'.'
Body = 'The file size of '+fileAbriv+' was greater than '+str(Statinfo.st_size)+'.'

if args.changesize >= str(statinfo.st_size):
    sendEmail(args.reciveEmail, SUBJECT, BODY)
elif args.changesize < str(statinfo.st_size):
    print (statinfo.st_size)
    print (fileFull)

if args.changesize >= str(Statinfo.st_size):
    sendEmail(args.reciveEmail, Subject, Body)
elif args.changesize < str(Statinfo.st_size):
    print (Statinfo.st_size)
    print (fileAbriv)
2 Years
Discussion Span
Last Post by abaddon2031

Apparently, the whole code depends on 2 values, fileFull and fileAbriv. So you can write a function

def func(fileFull, fileAbriv):

However, this code, with lines repeated with different variable name case is not good. There must be some other way to write your code. Read about the DRY principle.

In python, the normal way to walk a directory is to use os.walk(). I would do exactly this.


I figured it out i split it up into two seperate functions and then in the for statement called the correct function with the correct variables

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.