简体   繁体   English

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

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

I have a static folder and my main usage is a sub-directory there that is found in root/static/images/monkeys 我有一个静态文件夹,主要用途是在root / static / images / monkeys中找到的子目录

I have a flask app and I have a variable like so: 我有一个flask应用程序,并且有一个像这样的变量:

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

I use it in two functions, one function serves a static image in that folder, this function works: 我在两个函数中使用它,一个函数在该文件夹中提供静态图像,此函数有效:

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

I also have another function that uses this path, this function deletes images after X seconds from the folder 我还有另一个使用此路径的函数,该函数会在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)

This function does not work, when I run the server locally I get 当我在本地运行服务器时,此功能不起作用

 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'

I call the function like so: 我这样调用该函数:

remove_monkey_images(path=monkeys_folder_path)

Thanks. 谢谢。

Python function can have positional or keyword parameters . Python函数可以具有positionalkeyword 参数 Your function definition 您的功能定义

def remove_monkey_images(path)

describe function with one positional parameter. 用一个positional参数描述功能。 This function could be called only with one positional argument like 只能使用一个位置参数来调用此函数,例如

remove_monkey_images(monkeys_folder_path)

If you want to use keyword argument you needs 如果要使用keyword参数,则需要

def remove_monkey_images(path='/some_default_path')

In this case you could call function both with 在这种情况下,您可以使用

remove_monkey_images(monkeys_folder_path)

and

remove_monkey_images(path=monkeys_folder_path)

and

remove_monkey_images()

In latter case inside function argument path will have default value '/some_default_path'. 在后一种情况下,函数内部参数path将具有默认值'/ some_default_path'。

When you create the Timer , you have to pass it the called function's arguments, like this: 创建Timer ,必须将其传递给被调用函数的参数,如下所示:

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

Source 资源

As for the rest of your code I don't really know if it's consistent, but at least that's the cause of the error you're getting. 至于其余的代码,我实际上并不知道它是否一致,但这至少是导致错误的原因。

there is a syntax issue with your problem. 您的问题有语法问题。

Either do this: 要么这样做:

remove_monkey_images(monkeys_folder_path)

instead of 代替

remove_monkey_images(path=monkeys_folder_path)

or 要么

Update your function definition as: 将函数定义更新为:

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.

相关问题 类型错误:explore() 缺少 1 个必需的位置参数:“路径” - TypeError: explore() missing 1 required positional argument: 'path' Python 3异常:TypeError:函数缺少1个必需的位置参数:&#39;words&#39; - Python 3 Exception: TypeError: function missing 1 required positional argument: 'words' Python中的TypeError-缺少1个必需的位置参数 - TypeError in python - missing 1 required positional argument python 装饰器 TypeError 缺少 1 个必需的位置参数 - python decorator TypeError missing 1 required positional argument Python错误:TypeError:…缺少1个必需的位置参数: - Python ERROR: TypeError: … missing 1 required positional argument: Python 类型错误:CreateAccount() 缺少 1 个必需的位置参数 - Python TypeError: CreateAccount() missing 1 required positional argument Flask 显示 TypeError:send_from_directory() 缺少 1 个必需的位置参数:“路径” - Flask shows TypeError: send_from_directory() missing 1 required positional argument: 'path' Flask TypeError: __init__() 缺少 1 个必需的位置参数:&#39;status&#39; - Flask TypeError: __init__() missing 1 required positional argument: 'status' Flask 应用程序类型错误:__init__() 缺少 1 个必需的位置参数 - Flask Application TypeError: __init__() missing 1 required positional argument 类型错误:add_info() 缺少 1 个必需的位置参数:Flask 中的“作者” - TypeError: add_info() missing 1 required positional argument: 'writer' in Flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM