Python Forum

Full Version: create a new directory if not already exists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am able to upload files to a directory on my server using the following code :


for file in  file_list:
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
            flash('File(s) successfully uploaded')
I am setting UPLOAD_FOLDER in my core.py module :

UPLOAD_FOLDER = 'D:/DataUpload'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
What I would like to do is take the users email from the form as the directory with DataUpload and if it already exists then upload the files to it otherwise create the directory using the email address and then upload the files to it.

Is this possible?

Thanks for any advice/help

Fioranosnake
if you use pathlib, it's simple
>>> from pathlib import Path
>>> CurrentDir = Path('.')
>>>
>>> Newdir = CurrentDir / 'newdir'
>>> Newdir.mkdir(exist_ok=True)
newdir will only be created if it doesn't exist