Python Forum
create a new directory if not already exists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: create a new directory if not already exists (/thread-20739.html)



create a new directory if not already exists - fioranosnake - Aug-28-2019

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


RE: create a new directory if not already exists - Larz60+ - Aug-28-2019

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