简体   繁体   中英

Calling the same function multiple times with different arguments?

it's me again.

Now that my function is working properly I would like to call it multiple times while changing the arguments each time I call it.

This is my function

def my_function(output_name, input_dir):
with open(output_name, "w+") as f:
    os.chdir(input_dir)
    for fichiers in glob.glob("*"):
        today = datetime.datetime.today()
        modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(fichiers))
        duration = today - modified_date
        if duration.days < 5:
            f.write(f"{fichiers} = {duration} \n")

EDIT1: So after messing around with the solution given by Joao Santos, I've been able to get the input for the first item in the dictionary but thats it the second and third one don't seem to work at all. If put separately they work fine, i'm a little confused...

How can I achieve this result ?

Thank you very much !!

I don't know if I understood what you meant, but here goes ...

Add your arguments to a dictionary like :

arguments = {"output1.txt": "//10.2.30.61/c$\test/test\test", "output2.txt" : "//10.2.30.61/c$\test1/test1\test1", "output3.txt" : "//10.2.30.61/c$\test1/test1\test1"}

Then, simply iterate over that and call ur function:

for output_name, input_dir in arguments.items():
    my_function(output_name, input_dir)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM