Forums

[Errno 2] No such file or directory: 'static/images/Shekhar.jpg'

I'm trying to upload picture using the above code in Flask but getting the following exception while uploading : [Errno 2] No such file or directory: 'static/images/Shekhar.jpg'

UPLOAD_FOLDER = 'https://shekhuushashank.pythonanywhere.com/static/images/'
ALLOWED_EXTENSIONS = set(['jpg', 'png'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
@app.route('/avatar', methods = ['POST'])
@checklogin
def uploadavatar():
    try:
        if 'profile-pic' not in request.files:
            return 'No photo uploaded'
        file = request.files['profile-pic']
        if file.filename == '':
            return 'Invalid photo uploaded'
        if file and allowed_file(file.filename):
            if user.getAvatar() != 'avatar.png':
                os.remove(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(user.getAvatar())))
            #file.filename = createUID(user.getUsername()) + '.' + str(tuple(ALLOWED_EXTENSIONS.intersection(set(file.filename.rsplit('.'))))[0])
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(file.filename)))
            try:
                with UseDatabase(app.config['dbconfig']) as cursor:
                    _SQL = """UPDATE `users` SET `avatar`=%s WHERE `email`=%s"""
                    cursor.execute(_SQL, (file.filename, user.getEmail()))
                user.setAvatar(file.filename)
                return 'Avatar updated'
            except Exception as er:
                return str(er)
        else:
            return 'Uploaded file is not a picture file'
    except RequestEntityTooLarge as rete:
        return 'File too large'
    except Exception as er:
        return str(er)

Try using a full (absolute) path

Hey Conrad, I've tries using 'home/shekhuushashank/mysite/static/images/' as path but still the error is same.

To make it an absolute path, you need to put a "/" at the start -- eg. /home/shekhuushashank/mysite/static/images/

Thanks Giles, it worked :)

Excellent, thanks for confirming!

Hi.. I am having the same issue when trying to load the images for my page I get the same error even though I tried using the full absolute path.. Can someone help please? below is the error message and also the app.py code

File "/home/sams1983/mysite/app.py", line 118, in load_images for bg_img in os.listdir(directory): FileNotFoundError: [Errno 2] No such file or directory: 'https://www.cryptom1.com/home/sams1983/mysite/static/images/'

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

  directory = url_for('static', filename='')

  directory = directory[1:]

  print(directory)

  imgList = {}

  x = 0

  for bg_img in os.listdir(directory + 'images/'):

        f = os.path.join(directory + 'images/', bg_img)

        if os.path.isfile(f):

            if bg_img.endswith(".jpg") or bg_img.endswith(".jpeg"):

                x = x + 1

                imgList['img' + str(x)] = url_for('static', filename='images/' + bg_img)

  return jsonify(imgList)

that was the old code.. now I have changed it to this yet still getting the same error:

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

    directory = '/home/sams1983/mysite/static/images/'

    imgList = {}

    x = 0

    for bg_img in os.listdir(directory):

          f = os.path.join(directory, bg_img)

          if os.path.isfile(f):

              if bg_img.endswith(".jpg") or bg_img.endswith(".jpeg"):

                  x = x + 1

                  imgList['img' + str(x)] = directory + bg_img

    return jsonify(imgList)

[formatted by admin]

@sams1983 you need to configure static files mappings, see: https://help.pythonanywhere.com/pages/StaticFiles/.

Thanks Giles