简体   繁体   English

用不同的参数多次调用同一个函数?

[英]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. EDIT1:所以在搞乱了 Joao Santos 给出的解决方案之后,我已经能够获得字典中第一项的输入,但是第二和第三项似乎根本不起作用。 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:然后,简单地迭代它并调用 ur 函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用不同的参数多次调用相同的函数python - calling same functions in multiple times with different arguments python 在 Python (3.x) 中使用不同的参数连续多次调用函数? - Calling a function multiple times consecutively with different arguments in Python (3.x)? 多次不同时间调用同一个 function 的最佳做法是什么 - What is the best practice for calling the same function multiple different times 多次调用函数并传递参数 - Calling function and passing arguments multiple times 使用不同的参数多次运行python函数 - Run python function multiple times with different arguments Python 如何保持相同的运行 function 与不同的 arguments 在 ZDB974238714038DE63ZA7ACED1 中多次并行 - Python How to keep running same function multiple times parallel with different arguments in API 多次运行 function,每次运行使用不同的 arguments - Run a function multiple times with different arguments for each run 多次调用同一函数时,Python设计模式更好 - Better Python Design Pattern when calling same function multiple times 如何避免多次调用同一个函数python? - How to avoid calling same function multiple times python? Seaborn 在多次调用 function 时保存相同的数字 - Seaborn is saving the same figure when calling a function multiple times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM