Python Forum
create a new directory if not already exists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create a new directory if not already exists
#1
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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  create a default path with idle to a specific directory greybill 0 844 Apr-23-2023, 04:32 AM
Last Post: greybill
  p]Why os.path.exists("abc/d") and os.path.exists("abc/D") treat same rajeev1729 1 2,139 May-27-2020, 08:34 AM
Last Post: DeaD_EyE
  Mysql CREATE TABLE IF NOT EXISTS dynamic table name nisusavi 0 2,301 Apr-29-2020, 06:45 PM
Last Post: nisusavi
  Create a directory structure across many servers metro17 1 1,725 Nov-05-2019, 03:35 PM
Last Post: Larz60+
  Help repeately prompting for directory name if it already exists. hikerguy62 3 2,304 Aug-05-2019, 01:43 PM
Last Post: ichabod801
  Python Directory and File Create Error basic_learner 1 2,386 Dec-16-2018, 11:03 AM
Last Post: buran
  Windows/DOS python script cannot create a directory name beginning an @ at-sign LucRhan 6 5,984 May-08-2017, 08:03 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020