TypeError: function missing 1 required positional argument: 'path' Flask Python

TypeError: function missing 1 required positional argument: 'path' Flask Python

我有一个静态文件夹,我的主要用途是那里的一个子目录,它位于 root/static/images/monkeys

我有一个烧瓶应用程序,我有一个像这样的变量:

app = Flask(__name__)
monkeys_folder_path = os.path.join(app.static_folder, 'images', 'monkeys')

我在两个函数中使用它,一个函数在该文件夹中提供静态图像,这个函数有效:

@app.route('/monkey/<address>')
def serve_static(address):
    # this creates an image in /static/images/monkeys
    monkey_generator.create_monkey_from_address(address)
    filename = address + ".png"
    return send_from_directory(monkeys_folder_path,filename)

我还有另一个使用此路径的功能,此功能会在 X 秒后从文件夹中删除图像

def remove_monkey_images(path):
  threading.Timer(5.0, remove_monkey_images).start()
  # this function iterates in a loop over the files in the path and deletes them
  helper_functions.delete_images(path)

这个功能不起作用,当我运行本地服务器时我得到

 File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 1182, in run
    self.function(*self.args, **self.kwargs)
TypeError: remove_monkey_images() missing 1 required positional argument: 'path'

我这样调用函数:

remove_monkey_images(path=monkeys_folder_path)

谢谢。

您的问题存在语法问题。

要么这样做:

remove_monkey_images(monkeys_folder_path)

而不是

remove_monkey_images(path=monkeys_folder_path)

将函数定义更新为:

def remove_monkey_images(path=None):
  threading.Timer(5.0, remove_monkey_images).start()
  # this function iterates in a loop over the files in the path and deletes them
  helper_functions.delete_images(path)

Python函数可以有positionalkeywordparameters。你的函数定义

def remove_monkey_images(path)

用一个 positional 参数描述函数。这个函数只能用一个位置参数调用,比如

remove_monkey_images(monkeys_folder_path)

如果你想使用keyword参数你需要

def remove_monkey_images(path='/some_default_path')

在这种情况下,您可以使用

调用函数
remove_monkey_images(monkeys_folder_path)

remove_monkey_images(path=monkeys_folder_path)

remove_monkey_images()

在后一种情况下,函数参数 path 将具有默认值 '/some_default_path'。

当您创建 Timer 时,您必须将被调用函数的参数传递给它,如下所示:

threading.Timer(5.0, remove_monkey_images, (path,)).start()

Source

至于你的代码的其余部分,我真的不知道它是否一致,但至少这是你收到错误的原因。