我在运行python脚本时遇到语法错误?

时间:2019-07-19 08:17:22

标签: python python-3.x

在Windows OS上运行python 3脚本时遇到语法错误 在第tweet=f.filename

行出现语法错误
import pefile
import os
import array
import math
import pickle
from sklearn.externals import joblib
import sys
import argparse
import os, sys, shutil, time
import re
import pandas as pd
from flask import Flask request,jsonify,render_template,abort,redirect,url_for
from werkzeug import secure_filename

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/uploader', methods = ['GET', 'POST'])
def upload_file():

    if request.method == 'POST':

        f = request.files['file']
        f.save(secure_filename(f.filename))
        tweet=f.filename
        print(tweet)

1 个答案:

答案 0 :(得分:0)

我在Windows上尝试过,从'/ upload'端点返回文件上载html模板('tweet.html'),并在输出中获取文件名。当您访问/ uploader端点时,选择一个文件(例如“ bla4.txt”),然后单击“上传”按钮,文件名bla4.txt将显示在调试控制台中。

import os
import array
import math
import pickle
from sklearn.externals import joblib
import sys
import argparse
import os, sys, shutil, time
import re
import pandas as pd
from flask import Flask, request,jsonify,render_template,abort,redirect,url_for
import werkzeug

app = Flask(__name__)

@app.route('/')
def home():
    return '<h1>Hello World</h1>'

@app.route('/uploader', methods = ['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        f.save(werkzeug.secure_filename(f.filename))
        tweet=f.filename
        print(tweet)
    return render_template('tweet.html')

if __name__=='__main__':
    app.run(port='5000', debug=True)

templates / tweet.html

<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action='' method="POST" enctype="multipart/form-data">
    <p><input type='file' name='file' multiple=''>
    <input type='submit' value='upload'>
    </p>    
</form>

调试控制台输出:

127.0.0.1 - - [19/Jul/2019 14:07:41] "?[37mGET /uploader HTTP/1.1?[0m" 200 -
bla4.txt
127.0.0.1 - - [19/Jul/2019 14:07:48] "?[37mPOST /uploader HTTP/1.1?[0m" 200 -