简体   繁体   中英

I get a “FileNotFoundError” stating that my directory doesn't exist, even though it does?

I'm working on a database and in one of the stages I have to choose an image for the object in question. Here is what that page looks like when I submit a new object onto my database: New Object Image

And at the bottom you can see that my image: " bliss.jpg "

At this stage clicking the submit button should work but instead I get this error: *

FileNotFoundError: [Errno 2] No such file or directory: 'static/images\837ef32c-db6d-11ea-b034-186024e27139.jpg'

Which I thought was very strange considering I have a static/images folder in my directory.

I really am not sure what I'm doing wrong here. Here is my Python code

    @app.route("/new_camera", methods = ["GET", "POST"])
    def newcamera():
    connection=create_connection()
    if request.method =="POST":
        get = request.form
        Company = get["Company"]
        Model = get["Model"]
        PurchaseDate = get["PurchaseDate"]
        Condition = get["Condition"]
        KitLens = get["KitLens"]
        PurchasePrice = get["PurchasePrice"]

        #photo/image uploading=
        Picture = request.files["Picture"]
        picName = str(uuid.uuid1()) + os.path.splitext(Picture.filename)[1]
        Picture.save(os.path.join("static/images", picName))

and my HTML code

    <div class="form-group">

        <input name="Picture"  id="Picture" type="file" accept="image/*" capture />

    </div>

    <div class="form-group">
        <input type="submit" value="Submit" class="btn btn-info" />
    </div>

</form>

which is responsible for submitting my form. If you have anything I can do to try and fix this code, please let me know, thank you.

I should also point out that I do have " enctype=multipart/form-data " in my code.

Here is a good way to help diagnose relative path errors. Before .save() , try:

import os 
print( os.path.abspath(__file__) ) # path to current file
print( os.path.dirname(os.path.abspath(__file__)) ) # directory containing current file
print( os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ) # up one directory

With that, you can try something like:

print ( os.path.join( os.path.dirname(os.path.abspath(__file__)), os.path.join("static/images", picName)) )

to make sure your directory structure is as you expect.

What OS are you running?

Whats your HTTP server?

Is it possible to see the HTTP(S) request being created?

Try to do:

Picture = request.files["Picture"]
picName = str(uuid.uuid1()) + os.path.splitext(Picture.filename)[1].replace("/", ""\")
Picture.save(os.path.join("static/images", picName))

I would like to also point out, depending how your HTTP server is setup. It may not actively search within the current directory.

So instead of: path/to/the/page/directory/static/images/....

It will be: /static/images/....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM