繁体   English   中英

TypeError:函数缺少1个必需的位置参数:“ path”烧瓶Python

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

我有一个静态文件夹,主要用途是在root / static / images / monkeys中找到的子目录

我有一个flask应用程序,并且有一个像这样的变量:

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)

谢谢。

Python函数可以具有positionalkeyword 参数 您的功能定义

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()

资源

至于其余的代码,我实际上并不知道它是否一致,但这至少是导致错误的原因。

您的问题有语法问题。

要么这样做:

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)

暂无
暂无

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

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