[英]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函数可以具有
positional
或keyword
参数 。 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()
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.