1

I have this piece of code that needs to run each time I execute the program (it empties a folder):

import os

def ClearOutputFolder():
    ''' Clear 'Output/' directory '''
    for file in os.listdir('Output'):
        file_path = os.path.join('Output', file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
        except Exception as e:
            print(e)

ClearOutputFolder()

I was wondering if there's a less awkward way of automatically calling a function than defining it and then calling it later.

I've tried to put an __init__ outside of a class, just to see, but as expected it acted like a normal function and needed to be called.

import os

def __init__():
    delete_stuff                # this runs but does nothing on its own

It's not a matter of life and death, obviously, I was just curious if there's a simple solution that I'm not aware of.

Thanks.

EDITED for clarification

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Xartab
  • 37
  • 1
  • 7
  • 1
    could you explain what "build the program" means? – Paritosh Singh May 14 '19 at 15:59
  • 1
    Please consider adding a code sample, or revising the one you posted in this question. As it currently stands, its formatting and scope make it hard for us to help you; here is a [great resource](http://stackoverflow.com/help/mcve) to get you started on that. Good luck with your code! – Reblochon Masque May 14 '19 at 16:04
  • Are you maybe trying to autorun a function when a script is called? That could be accomplished by running it as `main()` in a `if __name__ == '__main__':` block – G. Anderson May 14 '19 at 16:07
  • @Paritosh sorry, I was thinking in SublimeText. I meant executing. – Xartab May 14 '19 at 16:20
  • if the code to call it is written in the file, it will run. if you dont call the function, it doesn't run. Just add a call on the function in the same file. – Paritosh Singh May 14 '19 at 16:24
  • @Paritosh I imagined. Thank you. – Xartab May 14 '19 at 16:36

1 Answers1

1

if you call the function in a if __name__ == '__main__ block, it will automatically execute upon launching the package.

import os

def ClearOutputFolder():
    ''' Clear 'Output/' directory '''
    for file in os.listdir('Output'):
        file_path = os.path.join('Output', file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
        except Exception as e:
            print(e)

def main():
    ClearOutputFolder()


if __name__ == '__main__':

    main()

if you want the call to happen upon importing, you can do like this:

import os

def ClearOutputFolder():
    ''' Clear 'Output/' directory '''
    for file in os.listdir('Output'):
        file_path = os.path.join('Output', file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
        except Exception as e:
            print(e)

ClearOutputFolder()   # this call is executed upon importing the package
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • I wanted the code to be more streamlined, and I guess this is a good solution. I could also add the function as a method to my class for the project, though to be honest this function doesn't belong there. Thank you for the reply. – Xartab May 14 '19 at 16:39