Murakma
Murakma

Reputation: 23

TypeError: convert() missing 1 required positional argument: 'filename'

I just got started on python. My aim is to build a program using flask that takes a csv file via file upload as an input, executes the code on the csv and the returns a new csv.

Whenever I try to run the code, I always get the following error when uploading a file:

TypeError: convert() missing 1 required positional argument: 'filename'

My code is as follows:

import os
from flask import Flask, render_template, redirect, request, send_from_directory, url_for
from werkzeug.utils import secure_filename

app = Flask(__name__)
ALLOWED_EXTENSIONS = set(["csv"])

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static/uploads')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

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


@app.route('/process', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('convert', filename=filename))

@app.route("/convert")
def convert(filename):
    with open (UPLOAD_FOLDER + filename, "r", encoding="UTF-8") as file:
        umlaute_dict = {
            'ä': 'ae',  # U+00E4	   \xc3\xa4
            'ö': 'oe',  # U+00F6	   \xc3\xb6
            'ü': 'ue',  # U+00FC	   \xc3\xbc
            'Ä': 'Ae',  # U+00C4	   \xc3\x84
            'Ö': 'Oe',  # U+00D6	   \xc3\x96
            'Ü': 'Ue',  # U+00DC	   \xc3\x9c
            'ß': 'ss',  # U+00DF	   \xc3\x9f
            '&#x2F': '',
        }
    with open(UPLOAD_FOLDER + 'csvfile.csv', 'w') as filetwo:
        for row in file:
            final = " "
            list = row.split(",")
            line = []
            firstname = list[5]
            for k in umlaute_dict:
                firstname = firstname.replace(k, umlaute_dict[k])
            title = list[8]
            for k in umlaute_dict:
                title = title.replace(k, umlaute_dict[k])
            line.append(firstname + ",")
            line.append(list[7] + ",")
            line.append(title + ",")
            line.append(list[9] + ",")
            final = "".join(line)
            print (final)
            filetwo.write(final)
            filetwo.write('\n')
            return redirect(url_for('uploaded_file', filename=filetwo))

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)


app.run(debug=True)

Thanks for your help!!

Best

Upvotes: 2

Views: 6122

Answers (1)

progmatico
progmatico

Reputation: 4964

Why do you think your convert will receive a filename parameter?

Something is missing here

@app.route("/convert")
def convert(filename):

Maybe you want to follow the same pattern of your code in

@app.route('/uploads/<filename>')
def uploaded_file(filename):

Upvotes: 2

Related Questions