0

I would like the script to create a new directory based on the given directories with the same name already in place. Such that if Folder 0 exists, but Folder 1 does not, it creates Folder 1 and stops at one directory creation.

import os

i=1
while True:
    path = "Folder_{}/".format(i)
    os.makedirs(os.path.dirname("Folder_{}/".format(i)), exist_ok=False)
    if not os.path.exists(path):
        os.makedirs(os.path.dirname("Folder_{}/".format(i)), exist_ok=False)
    i += 1

I have tested the above code, and it has quite given me the headache as it creates around 10K directories/second haha. Thanks in advance!

2
  • This is probably not a copy of your real code, as while: causes a syntax error. Commented Oct 13, 2018 at 21:39
  • Sorry, edited 'while True:'. I know I have no break in there but I cannot get it to compile with one. Commented Oct 13, 2018 at 21:54

1 Answer 1

1
import os
i=1
keepGoing=True
while keepGoing:
  path = "Folder_{}/".format(i)
  if not os.path.exists(path):
    os.makedirs(os.path.dirname("Folder_{}/".format(i)), exist_ok=False)
    keepGoing = False
  i += 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.