0

I am trying to create folders from a list. I can get it to create the folders but I want it to be be able to check if there are any folders missing and create the missing ones

Folders = ['Folder1','Test','Help']
for i in Folders
    os.makedirs(i)

This will create the folders but if i re-run it after deleting one it will just give me errors saying the first folder already exists. Is there any way to check and create the missing folders?

2 Answers 2

3

No need to check. Just use exist_ok=True argument in makedirs()

Reference: https://docs.python.org/3/library/os.html#os.makedirs

Sign up to request clarification or add additional context in comments.

Comments

1

To do this you can use and other function from os: os.path.isdir('folder name') which returns a bool: True if the file exists and False if it doesn't.

In your case:

Folders = ['Folder1','Test','Help']
for i in Folders:
    if not os.path.isdir(i):
        os.makedirs(i)

That is all :)

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.