Using python to make a single folder / Directory
Using python to make a single folder / Directory

The below codes are best and simple ways to make a easily using as regards in supplement to my previous post captioned Creating a folder using Python (https://shopinson.com/python/creating-a-folder-with-python/).

How to Make a Using Python

the below codes are tested, proved and sure codes to create a single folder in python without wasting time writing bulky codes just to implement a simple task of creating just “a” folder

Code Snippet Test 1 #Example 1

import os  os.mkdir('shopinson_/')

this code snippet throws FileExistsError if the ‘shopinson_directory‘ already exists in the current folder/directory 

Code snippet Test 2  #Example 2

from pathlib import Path  p = Path('shopinson_directory/') p.mkdir()

this second Test 2 #Example 2 throws FileExistsError as #Example 1

Now to ignore the FileExistsError been thrown, use the below code to create  a single folder in python

from pathlib import Path  p = Path('shopinson_directory') p.mkdir(exist_ok=True)

this FileExistsError is bypassed by defining exist_ok=True argument to .mkdir():

LEAVE A REPLY

Please enter your comment!
Please enter your name here