Forums

Uploading files from Desktop to PythonAnywhereFolder

Is there a way to upload files(s) from Desktop to PAW with a script?
Can I use BS4?

I'd recommend using our files API for that.

Thank you, I tried the files API, but it doesn't do what I'm trying to do. This is what I'm trying to do. For example.

I have a a file in my laptop called test.txt in c:\tony\Desktop\test.txt How can I upload this to /home/446list/grocery-list/test

Uploading a file Let’s upload a file. We’ll put the text hello, world into a file called foo.txt in your home directory:

resp = requests.post(
    urljoin(api_base, "files/path/home/{username}/foo.txt".format(username=username)),
    files={"content": "hello world"},
    headers={"Authorization": "Token {api_token}".format(api_token=api_token)}
)

Also while I was testing, I accidently deleted /user/446list/files/home/446list/grocery-list folder. Which is where my whole program is. How can I get that back?

Our backups are not designed for point-in-time recovery of user files, so it can take over a business day for the recovery. Send an email to support@pythonanywhere.com requesting the recovery with the full path to the file and the time (in UTC) when it was last in the state that you want it to be and we'll start the process.

Thanks, I'll send an email to support@pythonanywhere.com

But, is there a way to do this? Upload a file from "c:\tony\Desktop\test.txt" -----> /home/446list/grocery-list/test

Yes, absolutely. You would just need to read the contents of the local file into a string, and then upload it with code like the fragment you quoted earlier (in place of the "hello world" example), and you'd need to change the filename in the urljoin call above that.

My apologies but how do I read the content of the local file into a string? For example of a .png or .jpeg file? This is my first time attempting to upload from computer desktop to cloud (PAW).

Is the backslash / or \

Like this?

https://stackoverflow.com/questions/54147694/python-how-to-turn-an-image-into-a-string-and-back

import base64

with open("c:\tony\desktop\image.png", "rb") as image:
    b64string = base64.b64encode(image.read())

You can just pass the file object returned by open as the content:

with open("c:\tony\desktop\image.png", "rb") as image:
    resp = requests.post(
        urljoin(api_base, "files/path/home/{username}/foo.txt".format(username=username)),
        files={"content": image},
        headers={"Authorization": "Token {api_token}".format(api_token=api_token)}
    )

I'm sorry but I'm still figuring how to do this. I can't do with open("c:\tony\desktop\image.png" because the image is on my computer, and pythonanywhere can't access it from my computer"

From my flask app, when I upload, I'm only able to get the image.png file name.

I'm able to upload a file from my computer (desktop) to PAW if I run that script from my laptop. But not from PAW.

Yes. That code is to run on your computer to upload an image to PythonAnywhere.

Thank you, but is there a way to upload from my PAW website from my computer?

I realize this is not PAW related question, but any suggestion will be appreciated.

That first sentence does not make sense. What are you trying to do?

I have a pythonanywhere flask website. I have a form on it to upload images. How can I upload an image to my flask website using the form? The image is on my laptop desktop.

@446list it looks like a general programming question -- are you getting any specific errors on PythonAnywhere when you try to do it?

I'm sorry to say this, but it seem like who ever is replying, is not reading the full post. I think I made it very clear on exactly what I'm trying to do.

I think I can use ftp to upload a file. Is it possible to whitelist this site? ftp.gnu.org

Were you not able to upload your files via the files API?

Correct, I'm not able to upload a file (image) via the files API. I have a flask app with a form, the attachment = request.form.get("attachment") is the file upload. When I attempt to upload a file from my computer desktop from my flask app, it says FileNotFoundError: [Errno 2] No such file or directory: 'cats.jpg' Cats.jpg is an image file on my computer desktop. So how can I upload an image located on my computer desktop from my flask app running on pythonanywhere? I hope now its clear what I'm trying to do.

This is my code

with open(csv_file, 'a') as f_object:
        global id
        id+=1
        item = request.form.get("item")
        attachment = request.form.get("attachment")
        writer_object = writer(f_object)
        encoded_string =""
        if (attachment):
            path = directory + str(id)
            os.mkdir(path)
            with open(attachment, "rb") as image:
                resp = requests.post(
                    urljoin(api_base, "files/path/home/{username}/grocery-list/uploads/id"+(id+".png").format(username=username)),
                    files={"content": image},
                    headers={"Authorization": "Token {api_token}".format(api_token=api_token)}
                )
        List = [id, item]

This is the error log

2025-04-13 23:01:59,455: Exception on /insert [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/446list/grocery-list/appnew.py", line 52, in insert
    with open(attachment, "rb") as image:
FileNotFoundError: [Errno 2] No such file or directory: 'cats.jpg'

Cats.jpg and cats.jpg are two different file names.

Ok that was a typo. They both cats.jpg.

Still same issue, FileNotFoundError: [Errno 2] No such file or directory: 'cats.jpg'

Please read the whole post before replying. If the files api is not possible, I should be able to use something like ftplib to upload the file, correct? If not, any other solutions?

I'm very confused about what you're trying to do, unfortunately. In your code sample above, you are uploading files to your PythonAnywhere file storage using this URL:

"files/path/home/{username}/grocery-list/uploads/id"+(id+".png").format(username=username))

That would create a file in the grocery-list/uploads subdirectory of your home directory with a name of the format idSOMETHING.png, where the SOMETHING would be replaced by a number from the variable id in your code.

But in your stack trace from your Flask site, it's looking for a file called cats.jpg, which does not match that pattern.

You also seem to be talking about uploading files to your Flask app, but at the same time you're also showing code that is using the PythonAnywhere API to upload the files, which would not touch your Flask app.

Could you clarify exactly what you're trying to do? I have read the full forum thread and I'm puzzled.

Ok, let's start from the beginning. I have a file called cats.jpg on my laptop Desktop (click on the link below to see). How can I upload cats.jpg to PythonAnyWhere.com, from my flask app? The def insert() does everything it should do so far, except not uploading the cats.jpg when I click the insert button on my flask app. I hope this is clear. I updated my code to show cats.jpg, which matches with the file name on my Desktop. I also updated my code now to be cats.jpg, which matches the file name on the Desktop. But it still says the same error as before.

Screenshot showing cats.jpg on my laptop Desktop https://ibb.co/n8kStG4n

This is my updated code showing cats.jpg

@app.route('/insert', methods = ['GET', 'POST'])
def insert():
    with open(csv_file, 'a') as f_object:
        global id
        id+=1
        item = request.form.get("item")
        attachment = request.form.get("attachment")
        writer_object = writer(f_object)
        encoded_string =""
        if (attachment):
            path = directory + str(id)
            os.mkdir(path)
            with open(attachment, "rb") as image:
                resp = requests.post(
                    urljoin(api_base, "files/path/home/{username}/grocery-list/uploads/cats.jpg".format(username=username)),
                    files={"content": image},
                    headers={"Authorization": "Token {api_token}".format(api_token=api_token)}
                )
        List = [id, item]
        writer_object.writerow(List)
    with open(id_file, "w") as file:
        file.write(str(id))
    return redirect(url_for('index'))

This is the error message, FileNotFoundError: [Errno 2] No such file or directory: 'cats.jpg'

2025-04-15 01:06:55,136: Exception on /insert [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/446list/grocery-list/appnew.py", line 52, in insert
    with open(attachment, "rb") as image:
FileNotFoundError: [Errno 2] No such file or directory: 'cats.jpg'

Ok, if you want to create a Flask app that allows file uploads, you should follow the Flask docs: https://flask.palletsprojects.com/en/stable/patterns/fileuploads/ Here are the things you need to change from your current implementation:

  • In your HTML, make sure your <form> element has the enctype="multipart/form-data" attribute.
  • Your <input> tag needs to be of type="file"
  • To access the file in your insert function, make sure you use request.files instead of request.form
  • You then need to save the file to the path where you want it to be.

You do not need to use the files API. This is something we suggested when you asked about uploading a file via a script.