0

The following snippet send a file to the browser.

# Prepare selected file for download...
filename = request.form.get('filename')
filepath = '/home/nikos/wsgi/static/files/'

return send_from_directory( filepath, filename )

What if i want to print some lines before sending the file, as in:

# Prepare selected file for download...
pdata = pdata + '''Your file will be ready for download'''
padata = pdata + '''it will just atake a moment'''

filename = request.form.get('filename')
filepath = '/home/nikos/wsgi/static/files/'

return send_from_directory( filepath, filename )

if i try to add pdata + reponse like:

return pdata + send_from_directory( filepath, filename )

i get an error that return should return a string only not string + response

2 Answers2

2

If you want to return a string and a response object both, try this:

return (pdata, send_from_directory(filepath, filename))

This will return a tuple with both datatypes within. You can't use a + symbol unless you've got 2 objects that are compatible, as an example, "string" + "string" or (tuple,) + (tuple,)

UtahJarhead
  • 2,091
  • 1
  • 14
  • 21
  • The outer `()` are redundant and not needed. It would also be good to indicate how to retrieve the elements at the caller. – Stephen Rauch Sep 20 '18 at 02:32
  • Although this solution seems very straightforward i get this `TypeError: Invalid status argument` – Νικόλαος Βέργος Sep 20 '18 at 02:34
  • Also if i try `return send_from_directory(filepath, filename), pdata` i get `ValueError: unicode object contains non latin-1 characters` – Νικόλαος Βέργος Sep 20 '18 at 02:46
  • @StephenRauch Well, I think i knew that, but I like explicitly wrapping them in parentheses just so I can see at a glance. To be honest, I've never tried it without. – UtahJarhead Sep 20 '18 at 15:29
  • @ΝικόλαοςΒέργος Errors you are getting don't have anything to do with the `return` statement. You're getting the error probably in the `send_from_directory()` function and the data isn't what is expected. – UtahJarhead Sep 20 '18 at 15:30
  • Yes, its that function that gives trouble because it cannot handle a filename having chars other that latin-iso. Cn you help me fix this? – Νικόλαος Βέργος Sep 20 '18 at 15:57
  • I'll assume `filename` is the improper string. try this if python2: `send_from_directory(filepath, str(filename, 'utf-8'))` You're probably not using python3 because string are already unicode by default, I think. – UtahJarhead Sep 20 '18 at 16:41
-1

Why not just print before you return?

def add(a, b):
    print(f"{a} is being added...")
    print(f"{b} is being added..")
    return a + b

c = add(1, 2)
print(c)
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 strings.py
1 is being added...
2 is being added..
3

Suggest:

print(f'{pdata} "Your file will be ready for download..."')
print(f'{pdata} "it will just atake a moment..."')

filename = request.form.get('filename')
filepath = '/home/nikos/wsgi/static/files/'

return send_from_directory( filepath, filename )

Older Python:

print(f'{} "Your file will be ready for download..."'.format(pdata))
print(f'{} "it will just atake a moment..."'.format(pdata))

filename = request.form.get('filename')
filepath = '/home/nikos/wsgi/static/files/'

return send_from_directory( filepath, filename )
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20