从文件夹中获取随机文件并将其显示在烧瓶上的最简单方法是什么?

Whats the simplest way to get a random file from a folder and display it on flask?

这是我到目前为止尝试过的方法,但它一直说目录错误但我知道它是正确的 事实上它甚至打印了文件。这是我的代码

@app.route('/notok', methods=['GET'])
def asdfasd():
  path = "path has my irl name so i cant show it sorry ):"
  cats = random.choice([
      x for x in os.listdir(path)
      if os.path.isfile(os.path.join(path, x))
  ])
  print(cats)
  return flask.send_file(cats)

您的变量 cats 仅包含文件名,我相信您需要将完整路径发送至 flask.send_file。

@app.route('/notok', methods=['GET'])
def asdfasd():
  path='C:/tmp/'
  cats = random.choice([
      x for x in os.listdir(path)
      if os.path.isfile(os.path.join(path, x))
  ])
  print(cats)
  return(flask.send_file( (os.path.join(path, cats))))