- 1 year ago
Making multiple directories in python3
You can make multiple directories in Python 3 using the os.makedirs()
function. This function creates a directory recursively, including any intermediate directories that do not exist. Here's how you can use it:
pythonimport os
# List of directories to create
directories = ['dir1', 'dir2', 'dir3']
# Loop through the list and create directories
for directory in directories:
os.makedirs(directory)
print("Directories created successfully.")
In this example, we import the os
module and define a list called directories
containing the names of the directories we want to create. We then loop through the list and call os.makedirs()
for each directory name, which creates the directories one by one.
After running the code, you should see the message "Directories created successfully." and the specified directories will be created in the current working directory. If any of the directories already exist, the function will not raise an error and will simply skip creating that particular directory.