Check if a file already exists in a particular folder [duplicate]

Internet of Things IoT Frameworks 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ _x000D_ This question already has an answer here:_x000D_ _x000D_ _x000D_ How can I safely create a nested directory?_x000D_ _x000D_ 25 answers_x000D_ _x000D_ _x000D_ _x000D_ Safely create a file if and only if it does not exist with python_x000D_ _x000D_ 3 answers_x000D_ _x000D_ _x000D_ _x000D_ _x000D_ I want, before uploading a file in image folder, to check if the file in the folder already exists or not. If the file already exists it should show a message. from flask import Flask, render_template, request from werkzeug import secure_filename UPLOAD_FOLDER = '/path/to/the/uploads' ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'GIF']) app = Flask(__name__) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER import os, os.path APP_ROOT = os.path.dirname(os.path.abspath(__file__)) UPLOAD_FOLD = '/python/image/' UPLOAD_FOLDER = os.path.join(APP_ROOT, UPLOAD_FOLD) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER @app.route('/upload') def load_file(): return render_template('upload.html') @app.route('/uploader', methods = ['GET', 'POST']) def upload_file(): if request.method == 'POST': f = request.files['file'] f.save(os.path.join(app.config['UPLOAD_FOLDER'],secure_filename(f.filename))) return 'file uploaded successfully' if __name__ == '__main__': app.run(debug = True)

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ You can check whether a path exists or not by using os.path.exists method, it returns True if the path passed as a parameter exists and returns False if it doesn't. For instance: If you want to check if there is any file named hello.c in the current working directory, then you can use the following code: from os import path path.exists('hello.c') # This will return True if hello.c exists and will return False if it doesn't Or You can use the absolute path too For instance: If you want to check if there is any folder named Users in your C drive then you can use the following code: from os import path path.exists('C:\Users') # This will return True if Users exists in C drive and will return False if it doesn't

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links