Skip to main content
 首页 » 编程设计

python之获取上传到 Flask 的文件路径

2024年10月01日11bjzhanghao

<分区>

我有一个 Python 网络应用程序,它在网络浏览器中本地运行以方便界面,处理用户选择的文件并保存处理后的数据。我需要添加在选择文件的同一文件夹中创建新子目录的功能(即,如果文件是 path/fname.txt,我需要创建 path/fname/,我可以在其中放置处理过的数据)。如何访问此文件路径?

在其他项目中,我单独请求路径,但这很笨拙,我不想让用户为每个文件执行这个无关的步骤。

@app.route('/getthefile', methods=['POST']) 
def getthefile(): 
    file = request.files['myfile'] 
    filename = secure_filename(file.filename) 
    new_path = os.path.abspath(filename) 
    # gives me the wrong path